Path: blob/main/crates/polars-ops/src/chunked_array/list/dispersion.rs
6939 views
use arrow::temporal_conversions::MICROSECONDS_IN_DAY as US_IN_DAY;12use super::*;34pub(super) fn median_with_nulls(ca: &ListChunked) -> Series {5match ca.inner_dtype() {6DataType::Float32 => {7let out: Float32Chunked = ca8.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))9.with_name(ca.name().clone());10out.into_series()11},12#[cfg(feature = "dtype-datetime")]13DataType::Date => {14let out: Int64Chunked = ca15.apply_amortized_generic(|s| {16s.and_then(|s| s.as_ref().median().map(|v| (v * (US_IN_DAY as f64)) as i64))17})18.with_name(ca.name().clone());19out.into_datetime(TimeUnit::Microseconds, None)20.into_series()21},22dt if dt.is_temporal() => {23let out: Int64Chunked = ca24.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))25.with_name(ca.name().clone());26out.cast(dt).unwrap()27},28_ => {29let out: Float64Chunked = ca30.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))31.with_name(ca.name().clone());32out.into_series()33},34}35}3637pub(super) fn std_with_nulls(ca: &ListChunked, ddof: u8) -> Series {38match ca.inner_dtype() {39DataType::Float32 => {40let out: Float32Chunked = ca41.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))42.with_name(ca.name().clone());43out.into_series()44},45#[cfg(feature = "dtype-duration")]46DataType::Duration(tu) => {47let out: Int64Chunked = ca48.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))49.with_name(ca.name().clone());50out.into_duration(*tu).into_series()51},52_ => {53let out: Float64Chunked = ca54.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof)))55.with_name(ca.name().clone());56out.into_series()57},58}59}6061pub(super) fn var_with_nulls(ca: &ListChunked, ddof: u8) -> PolarsResult<Series> {62match ca.inner_dtype() {63DataType::Float32 => {64let out: Float32Chunked = ca65.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))66.with_name(ca.name().clone());67Ok(out.into_series())68},69dt if dt.is_temporal() => {70polars_bail!(InvalidOperation: "variance of type {dt} is not supported")71},72_ => {73let out: Float64Chunked = ca74.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))75.with_name(ca.name().clone());76Ok(out.into_series())77},78}79}808182