Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list & array measures of dispersion #13245

Merged
merged 18 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/polars-core/src/series/implementations/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ impl SeriesTrait for SeriesWrap<DurationChunked> {
self.0.median()
}

fn std(&self, ddof: u8) -> Option<f64> {
self.0.std(ddof)
}

fn var(&self, ddof: u8) -> Option<f64> {
self.0.var(ddof)
}

fn append(&mut self, other: &Series) -> PolarsResult<()> {
polars_ensure!(self.0.dtype() == other.dtype(), append);
let other = other.to_physical_repr().into_owned();
Expand Down Expand Up @@ -419,10 +427,11 @@ impl SeriesTrait for SeriesWrap<DurationChunked> {
fn var_as_series(&self, ddof: u8) -> PolarsResult<Series> {
Ok(self
.0
.cast_time_unit(TimeUnit::Milliseconds)
.var_as_series(ddof)
.cast(&self.dtype().to_physical())
.unwrap()
.into_duration(self.0.time_unit()))
.into_duration(TimeUnit::Milliseconds))
}
fn median_as_series(&self) -> PolarsResult<Series> {
Ok(self
Expand Down
8 changes: 8 additions & 0 deletions crates/polars-core/src/series/implementations/floats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,14 @@ macro_rules! impl_dyn_series {
self.0.median().map(|v| v as f64)
}

fn std(&self, ddof: u8) -> Option<f64> {
self.0.std(ddof)
}

fn var(&self, ddof: u8) -> Option<f64> {
self.0.var(ddof)
}

#[cfg(feature = "chunked_ids")]
unsafe fn _take_chunked_unchecked(&self, by: &[ChunkId], sorted: IsSorted) -> Series {
self.0.take_chunked_unchecked(by, sorted).into_series()
Expand Down
8 changes: 8 additions & 0 deletions crates/polars-core/src/series/implementations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ macro_rules! impl_dyn_series {
self.0.median()
}

fn std(&self, ddof: u8) -> Option<f64> {
self.0.std(ddof)
}

fn var(&self, ddof: u8) -> Option<f64> {
self.0.var(ddof)
}

#[cfg(feature = "chunked_ids")]
unsafe fn _take_chunked_unchecked(&self, by: &[ChunkId], sorted: IsSorted) -> Series {
self.0.take_chunked_unchecked(by, sorted).into_series()
Expand Down
12 changes: 12 additions & 0 deletions crates/polars-core/src/series/series_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,18 @@ pub trait SeriesTrait:
None
}

/// Returns the std value in the array
/// Returns an option because the array is nullable.
fn std(&self, _ddof: u8) -> Option<f64> {
None
}

/// Returns the var value in the array
/// Returns an option because the array is nullable.
fn var(&self, _ddof: u8) -> Option<f64> {
None
}

/// Returns the median value in the array
/// Returns an option because the array is nullable.
fn median(&self) -> Option<f64> {
Expand Down
96 changes: 96 additions & 0 deletions crates/polars-ops/src/chunked_array/array/dispersion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
use polars_core::datatypes::ArrayChunked;

use super::*;

pub(super) fn median_with_nulls(ca: &ArrayChunked) -> PolarsResult<Series> {
let mut out = match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(tu) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(tu).into_series()
},
_ => {
let out: Float64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))
.with_name(ca.name());
out.into_series()
},
};
out.rename(ca.name());
Ok(out)
}

pub(super) fn std_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
let mut out = match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(tu) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(tu).into_series()
},
_ => {
let out: Float64Chunked = ca
.amortized_iter()
.map(|s| s.and_then(|s| s.as_ref().std(ddof)))
.collect();
out.into_series()
},
};
out.rename(ca.name());
Ok(out)
}

pub(super) fn var_with_nulls(ca: &ArrayChunked, ddof: u8) -> PolarsResult<Series> {
let mut out = match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(TimeUnit::Milliseconds) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(TimeUnit::Milliseconds).into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(TimeUnit::Microseconds | TimeUnit::Nanoseconds) => {
let out: Int64Chunked = ca
.cast(&DataType::Array(
Box::new(DataType::Duration(TimeUnit::Milliseconds)),
ca.width(),
))
.unwrap()
.array()
.unwrap()
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(TimeUnit::Milliseconds).into_series()
},
_ => {
let out: Float64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))
.with_name(ca.name());
out.into_series()
},
};
out.rename(ca.name());
Ok(out)
}
1 change: 1 addition & 0 deletions crates/polars-ops/src/chunked_array/array/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(feature = "array_any_all")]
mod any_all;
mod count;
mod dispersion;
mod get;
mod join;
mod min_max;
Expand Down
15 changes: 15 additions & 0 deletions crates/polars-ops/src/chunked_array/array/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ pub trait ArrayNameSpace: AsArray {
}
}

fn array_median(&self) -> PolarsResult<Series> {
let ca = self.as_array();
dispersion::median_with_nulls(ca)
}

fn array_std(&self, ddof: u8) -> PolarsResult<Series> {
let ca = self.as_array();
dispersion::std_with_nulls(ca, ddof)
}

fn array_var(&self, ddof: u8) -> PolarsResult<Series> {
let ca = self.as_array();
dispersion::var_with_nulls(ca, ddof)
}

fn array_unique(&self) -> PolarsResult<ListChunked> {
let ca = self.as_array();
ca.try_apply_amortized_to_list(|s| s.as_ref().unique())
Expand Down
88 changes: 88 additions & 0 deletions crates/polars-ops/src/chunked_array/list/dispersion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use polars_core::datatypes::ListChunked;

use super::*;

pub(super) fn median_with_nulls(ca: &ListChunked) -> Series {
return match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(tu) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median().map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(tu).into_series()
},
_ => {
ion-elgreco marked this conversation as resolved.
Show resolved Hide resolved
let out: Float64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().median()))
.with_name(ca.name());
out.into_series()
},
};
}

pub(super) fn std_with_nulls(ca: &ListChunked, ddof: u8) -> Series {
return match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(tu) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(tu).into_series()
},
_ => {
let out: Float64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().std(ddof)))
.with_name(ca.name());
out.into_series()
},
};
}

pub(super) fn var_with_nulls(ca: &ListChunked, ddof: u8) -> Series {
return match ca.inner_dtype() {
DataType::Float32 => {
let out: Float32Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as f32)))
.with_name(ca.name());
out.into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(TimeUnit::Milliseconds) => {
let out: Int64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(TimeUnit::Milliseconds).into_series()
},
#[cfg(feature = "dtype-duration")]
DataType::Duration(TimeUnit::Microseconds | TimeUnit::Nanoseconds) => {
let out: Int64Chunked = ca
.cast(&DataType::List(Box::new(DataType::Duration(
TimeUnit::Milliseconds,
))))
.unwrap()
.list()
.unwrap()
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof).map(|v| v as i64)))
.with_name(ca.name());
out.into_duration(TimeUnit::Milliseconds).into_series()
},
_ => {
let out: Float64Chunked = ca
.apply_amortized_generic(|s| s.and_then(|s| s.as_ref().var(ddof)))
.with_name(ca.name());
out.into_series()
},
};
}
1 change: 1 addition & 0 deletions crates/polars-ops/src/chunked_array/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use polars_core::prelude::*;
#[cfg(feature = "list_any_all")]
mod any_all;
mod count;
mod dispersion;
#[cfg(feature = "hash")]
pub(crate) mod hash;
mod min_max;
Expand Down
15 changes: 15 additions & 0 deletions crates/polars-ops/src/chunked_array/list/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,21 @@ pub trait ListNameSpaceImpl: AsList {
}
}

fn lst_median(&self) -> Series {
let ca = self.as_list();
dispersion::median_with_nulls(ca)
}

fn lst_std(&self, ddof: u8) -> Series {
let ca = self.as_list();
dispersion::std_with_nulls(ca, ddof)
}

fn lst_var(&self, ddof: u8) -> Series {
let ca = self.as_list();
dispersion::var_with_nulls(ca, ddof)
}

fn same_type(&self, out: ListChunked) -> ListChunked {
let ca = self.as_list();
let dtype = ca.dtype();
Expand Down
18 changes: 18 additions & 0 deletions crates/polars-plan/src/dsl/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ impl ArrayNameSpace {
.map_private(FunctionExpr::ArrayExpr(ArrayFunction::Sum))
}

/// Compute the std of the items in every subarray.
pub fn std(self, ddof: u8) -> Expr {
self.0
.map_private(FunctionExpr::ArrayExpr(ArrayFunction::Std(ddof)))
}

/// Compute the var of the items in every subarray.
pub fn var(self, ddof: u8) -> Expr {
self.0
.map_private(FunctionExpr::ArrayExpr(ArrayFunction::Var(ddof)))
}

/// Compute the median of the items in every subarray.
pub fn median(self) -> Expr {
self.0
.map_private(FunctionExpr::ArrayExpr(ArrayFunction::Median))
}

/// Keep only the unique values in every sub-array.
pub fn unique(self) -> Expr {
self.0
Expand Down
Loading