Path: blob/main/crates/polars-ops/src/chunked_array/array/dispersion.rs
8395 views
use num_traits::FromPrimitive;1use polars_utils::float16::pf16;23use super::*;45pub(super) fn mean_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {6let mut out = match ca.inner_dtype() {7#[cfg(feature = "dtype-f16")]8DataType::Float16 => {9let out: Float16Chunked = ca10.apply_amortized_generic(|s| {11s.and_then(|s| s.as_ref().mean().map(|v| pf16::from_f64(v).unwrap()))12})13.with_name(ca.name().clone());14out.into_series()15},16DataType::Float32 => {17let out: Float32Chunked = ca18.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as f32)))19.with_name(ca.name().clone());20out.into_series()21},22#[cfg(feature = "dtype-duration")]23DataType::Duration(tu) => {24let out: Int64Chunked = ca25.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean().map(|v| v as i64)))26.with_name(ca.name().clone());27out.into_duration(*tu).into_series()28},29_ => {30let out: Float64Chunked = ca31.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().mean()))32.with_name(ca.name().clone());33out.into_series()34},35};36out.rename(ca.name().clone());37Ok(out)38}3940pub(super) fn median_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {41let mut out = match ca.inner_dtype() {42#[cfg(feature = "dtype-f16")]43DataType::Float16 => {44let out: Float16Chunked = ca45.apply_amortized_generic(|s| {46s.and_then(|s| s.as_ref().median().map(|v| pf16::from_f64(v).unwrap()))47})48.with_name(ca.name().clone());49out.into_series()50},51DataType::Float32 => {52let out: Float32Chunked = ca53.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))54.with_name(ca.name().clone());55out.into_series()56},57#[cfg(feature = "dtype-duration")]58DataType::Duration(tu) => {59let out: Int64Chunked = ca60.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))61.with_name(ca.name().clone());62out.into_duration(*tu).into_series()63},64_ => {65let out: Float64Chunked = ca66.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))67.with_name(ca.name().clone());68out.into_series()69},70};71out.rename(ca.name().clone());72Ok(out)73}7475pub(super) fn std_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {76let mut out = match ca.inner_dtype() {77#[cfg(feature = "dtype-f16")]78DataType::Float16 => {79let out: Float16Chunked = ca80.apply_amortized_generic(|s| {81s.and_then(|s| s.as_ref().std(ddof).map(|v| pf16::from_f64(v).unwrap()))82})83.with_name(ca.name().clone());84out.into_series()85},86DataType::Float32 => {87let out: Float32Chunked = ca88.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))89.with_name(ca.name().clone());90out.into_series()91},92#[cfg(feature = "dtype-duration")]93DataType::Duration(tu) => {94let out: Int64Chunked = ca95.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))96.with_name(ca.name().clone());97out.into_duration(*tu).into_series()98},99_ => {100let out: Float64Chunked = {101ca.amortized_iter()102.map(|s| s.and_then(|s| s.as_ref().std(ddof)))103.collect()104};105out.into_series()106},107};108out.rename(ca.name().clone());109Ok(out)110}111112pub(super) fn var_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {113let mut out = match ca.inner_dtype() {114#[cfg(feature = "dtype-f16")]115DataType::Float16 => {116let out: Float16Chunked = ca117.apply_amortized_generic(|s| {118s.and_then(|s| s.as_ref().var(ddof).map(|v| pf16::from_f64(v).unwrap()))119})120.with_name(ca.name().clone());121out.into_series()122},123DataType::Float32 => {124let out: Float32Chunked = ca125.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))126.with_name(ca.name().clone());127out.into_series()128},129#[cfg(feature = "dtype-duration")]130DataType::Duration(TimeUnit::Milliseconds) => {131let out: Int64Chunked = ca132.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))133.with_name(ca.name().clone());134out.into_duration(TimeUnit::Milliseconds).into_series()135},136#[cfg(feature = "dtype-duration")]137DataType::Duration(TimeUnit::Microseconds | TimeUnit::Nanoseconds) => {138let out: Int64Chunked = ca139.cast(&DataType::Array(140Box::new(DataType::Duration(TimeUnit::Milliseconds)),141ca.width(),142))143.unwrap()144.array()145.unwrap()146.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))147.with_name(ca.name().clone());148out.into_duration(TimeUnit::Milliseconds).into_series()149},150_ => {151let out: Float64Chunked = ca152.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))153.with_name(ca.name().clone());154out.into_series()155},156};157out.rename(ca.name().clone());158Ok(out)159}160161162