diff --git a/crates/polars-core/Cargo.toml b/crates/polars-core/Cargo.toml index afecf3c5a88e..12a8a536ebdd 100644 --- a/crates/polars-core/Cargo.toml +++ b/crates/polars-core/Cargo.toml @@ -91,7 +91,6 @@ take_opt_iter = [] group_by_list = [] # rolling window functions rolling_window = [] -diff = [] moment = [] diagonal_concat = [] horizontal_concat = [] @@ -147,7 +146,6 @@ docs-selection = [ "dot_product", "row_hash", "rolling_window", - "diff", "moment", "dtype-categorical", "dtype-decimal", diff --git a/crates/polars-core/src/series/ops/diff.rs b/crates/polars-core/src/series/ops/diff.rs deleted file mode 100644 index b3452ccade81..000000000000 --- a/crates/polars-core/src/series/ops/diff.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::prelude::*; -use crate::series::ops::NullBehavior; - -impl Series { - pub fn diff(&self, n: i64, null_behavior: NullBehavior) -> PolarsResult { - use DataType::*; - let s = match self.dtype() { - UInt8 => self.cast(&Int16).unwrap(), - UInt16 => self.cast(&Int32).unwrap(), - UInt32 | UInt64 => self.cast(&Int64).unwrap(), - _ => self.clone(), - }; - - match null_behavior { - NullBehavior::Ignore => Ok(&s - &s.shift(n)), - NullBehavior::Drop => { - polars_ensure!(n > 0, InvalidOperation: "only positive integer allowed if nulls are dropped in 'diff' operation"); - let n = n as usize; - let len = s.len() - n; - Ok(&self.slice(n as i64, len) - &s.slice(0, len)) - }, - } - } -} diff --git a/crates/polars-core/src/series/ops/mod.rs b/crates/polars-core/src/series/ops/mod.rs index bc57ad3ee480..48766748f58b 100644 --- a/crates/polars-core/src/series/ops/mod.rs +++ b/crates/polars-core/src/series/ops/mod.rs @@ -1,5 +1,3 @@ -#[cfg(feature = "diff")] -pub mod diff; mod downcast; mod extend; #[cfg(feature = "moment")] diff --git a/crates/polars-ops/Cargo.toml b/crates/polars-ops/Cargo.toml index 8e5c2aaa25c3..1ea76b197cd5 100644 --- a/crates/polars-ops/Cargo.toml +++ b/crates/polars-ops/Cargo.toml @@ -82,8 +82,8 @@ to_dummies = [] interpolate = [] list_to_struct = ["polars-core/dtype-struct"] list_count = [] -diff = ["polars-core/diff"] -pct_change = ["polars-core/diff"] +diff = [] +pct_change = ["diff"] strings = ["polars-core/strings"] string_justify = ["polars-core/strings"] string_from_radix = ["polars-core/strings"] diff --git a/crates/polars-ops/src/chunked_array/list/namespace.rs b/crates/polars-ops/src/chunked_array/list/namespace.rs index f9f92a75ad85..37165179d187 100644 --- a/crates/polars-ops/src/chunked_array/list/namespace.rs +++ b/crates/polars-ops/src/chunked_array/list/namespace.rs @@ -17,6 +17,8 @@ use super::*; use crate::chunked_array::list::any_all::*; use crate::chunked_array::list::min_max::{list_max_function, list_min_function}; use crate::chunked_array::list::sum_mean::sum_with_nulls; +#[cfg(feature = "diff")] +use crate::prelude::diff; use crate::prelude::list::sum_mean::{mean_list_numerical, sum_list_numerical}; use crate::series::ArgAgg; @@ -256,7 +258,7 @@ pub trait ListNameSpaceImpl: AsList { #[cfg(feature = "diff")] fn lst_diff(&self, n: i64, null_behavior: NullBehavior) -> PolarsResult { let ca = self.as_list(); - ca.try_apply_amortized(|s| s.as_ref().diff(n, null_behavior)) + ca.try_apply_amortized(|s| diff(s.as_ref(), n, null_behavior)) } fn lst_shift(&self, periods: &Series) -> PolarsResult { diff --git a/crates/polars-ops/src/series/ops/diff.rs b/crates/polars-ops/src/series/ops/diff.rs new file mode 100644 index 000000000000..8fa28768609e --- /dev/null +++ b/crates/polars-ops/src/series/ops/diff.rs @@ -0,0 +1,22 @@ +use polars_core::prelude::*; +use polars_core::series::ops::NullBehavior; + +pub fn diff(s: &Series, n: i64, null_behavior: NullBehavior) -> PolarsResult { + use DataType::*; + let s = match s.dtype() { + UInt8 => s.cast(&Int16)?, + UInt16 => s.cast(&Int32)?, + UInt32 | UInt64 => s.cast(&Int64)?, + _ => s.clone(), + }; + + match null_behavior { + NullBehavior::Ignore => Ok(&s - &s.shift(n)), + NullBehavior::Drop => { + polars_ensure!(n > 0, InvalidOperation: "only positive integer allowed if nulls are dropped in 'diff' operation"); + let n = n as usize; + let len = s.len() - n; + Ok(&s.slice(n as i64, len) - &s.slice(0, len)) + }, + } +} diff --git a/crates/polars-ops/src/series/ops/mod.rs b/crates/polars-ops/src/series/ops/mod.rs index 6437c7a0ffac..b771520bd192 100644 --- a/crates/polars-ops/src/series/ops/mod.rs +++ b/crates/polars-ops/src/series/ops/mod.rs @@ -7,6 +7,8 @@ mod clip; mod cum_agg; #[cfg(feature = "cutqcut")] mod cut; +#[cfg(feature = "diff")] +mod diff; #[cfg(feature = "ewma")] mod ewm; #[cfg(feature = "round_series")] @@ -49,6 +51,8 @@ pub use clip::*; pub use cum_agg::*; #[cfg(feature = "cutqcut")] pub use cut::*; +#[cfg(feature = "diff")] +pub use diff::*; #[cfg(feature = "ewma")] pub use ewm::*; #[cfg(feature = "round_series")] diff --git a/crates/polars-ops/src/series/ops/pct_change.rs b/crates/polars-ops/src/series/ops/pct_change.rs index 24df891b382e..56c7af142e9b 100644 --- a/crates/polars-ops/src/series/ops/pct_change.rs +++ b/crates/polars-ops/src/series/ops/pct_change.rs @@ -1,6 +1,8 @@ use polars_core::prelude::*; use polars_core::series::ops::NullBehavior; +use crate::prelude::diff; + pub fn pct_change(s: &Series, n: &Series) -> PolarsResult { polars_ensure!( n.len() == 1, @@ -16,9 +18,7 @@ pub fn pct_change(s: &Series, n: &Series) -> PolarsResult { let n_s = n.cast(&DataType::Int64)?; if let Some(n) = n_s.i64()?.get(0) { - fill_null_s - .diff(n, NullBehavior::Ignore)? - .divide(&fill_null_s.shift(n)) + diff(&fill_null_s, n, NullBehavior::Ignore)?.divide(&fill_null_s.shift(n)) } else { Ok(Series::full_null(s.name(), s.len(), s.dtype())) } diff --git a/crates/polars-plan/Cargo.toml b/crates/polars-plan/Cargo.toml index ee412f48dbec..99fbd668c782 100644 --- a/crates/polars-plan/Cargo.toml +++ b/crates/polars-plan/Cargo.toml @@ -107,7 +107,7 @@ rolling_window = [ "polars-time/rolling_window", ] rank = ["polars-ops/rank"] -diff = ["polars-core/diff", "polars-ops/diff"] +diff = ["polars-ops/diff"] pct_change = ["polars-ops/pct_change"] moment = ["polars-core/moment", "polars-ops/moment"] abs = ["polars-core/abs"] diff --git a/crates/polars-plan/src/dsl/function_expr/dispatch.rs b/crates/polars-plan/src/dsl/function_expr/dispatch.rs index 4b3c5f14c1e3..eb00120e970f 100644 --- a/crates/polars-plan/src/dsl/function_expr/dispatch.rs +++ b/crates/polars-plan/src/dsl/function_expr/dispatch.rs @@ -15,7 +15,7 @@ pub(super) fn approx_n_unique(s: &Series) -> PolarsResult { #[cfg(feature = "diff")] pub(super) fn diff(s: &Series, n: i64, null_behavior: NullBehavior) -> PolarsResult { - s.diff(n, null_behavior) + polars_ops::prelude::diff(s, n, null_behavior) } #[cfg(feature = "pct_change")] diff --git a/crates/polars/Cargo.toml b/crates/polars/Cargo.toml index 58f6a8334bcd..3ca2884540ca 100644 --- a/crates/polars/Cargo.toml +++ b/crates/polars/Cargo.toml @@ -143,7 +143,7 @@ cum_agg = ["polars-ops/cum_agg", "polars-lazy?/cum_agg"] rolling_window = ["polars-core/rolling_window", "polars-lazy?/rolling_window", "polars-time/rolling_window"] interpolate = ["polars-ops/interpolate", "polars-lazy?/interpolate"] rank = ["polars-lazy?/rank", "polars-ops/rank"] -diff = ["polars-core/diff", "polars-lazy?/diff", "polars-ops/diff"] +diff = ["polars-ops/diff", "polars-lazy?/diff"] pct_change = ["polars-ops/pct_change", "polars-lazy?/pct_change"] moment = ["polars-core/moment", "polars-lazy?/moment", "polars-ops/moment"] range = ["polars-lazy?/range"] diff --git a/crates/polars/src/lib.rs b/crates/polars/src/lib.rs index c04fb989e47d..df7da1257ff1 100644 --- a/crates/polars/src/lib.rs +++ b/crates/polars/src/lib.rs @@ -282,7 +282,7 @@ //! [`cummin`]: polars_ops::prelude::cummin //! [`cummax`]: polars_ops::prelude::cummax //! [`rolling_mean`]: crate::series::Series#method.rolling_mean -//! [`diff`]: crate::series::Series::diff +//! [`diff`]: polars_ops::prelude::diff //! [`List`]: crate::datatypes::DataType::List //! [`Struct`]: crate::datatypes::DataType::Struct //!