Skip to content

Commit

Permalink
chore(rust): Move diff to polars-ops (#11818)
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa authored Oct 18, 2023
1 parent c3e1c1e commit 9ea46ef
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 38 deletions.
2 changes: 0 additions & 2 deletions crates/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ take_opt_iter = []
group_by_list = []
# rolling window functions
rolling_window = []
diff = []
moment = []
diagonal_concat = []
horizontal_concat = []
Expand Down Expand Up @@ -147,7 +146,6 @@ docs-selection = [
"dot_product",
"row_hash",
"rolling_window",
"diff",
"moment",
"dtype-categorical",
"dtype-decimal",
Expand Down
24 changes: 0 additions & 24 deletions crates/polars-core/src/series/ops/diff.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/polars-core/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[cfg(feature = "diff")]
pub mod diff;
mod downcast;
mod extend;
#[cfg(feature = "moment")]
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 3 additions & 1 deletion crates/polars-ops/src/chunked_array/list/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -256,7 +258,7 @@ pub trait ListNameSpaceImpl: AsList {
#[cfg(feature = "diff")]
fn lst_diff(&self, n: i64, null_behavior: NullBehavior) -> PolarsResult<ListChunked> {
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<ListChunked> {
Expand Down
22 changes: 22 additions & 0 deletions crates/polars-ops/src/series/ops/diff.rs
Original file line number Diff line number Diff line change
@@ -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<Series> {
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))
},
}
}
4 changes: 4 additions & 0 deletions crates/polars-ops/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")]
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-ops/src/series/ops/pct_change.rs
Original file line number Diff line number Diff line change
@@ -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<Series> {
polars_ensure!(
n.len() == 1,
Expand All @@ -16,9 +18,7 @@ pub fn pct_change(s: &Series, n: &Series) -> PolarsResult<Series> {

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()))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(super) fn approx_n_unique(s: &Series) -> PolarsResult<Series> {

#[cfg(feature = "diff")]
pub(super) fn diff(s: &Series, n: i64, null_behavior: NullBehavior) -> PolarsResult<Series> {
s.diff(n, null_behavior)
polars_ops::prelude::diff(s, n, null_behavior)
}

#[cfg(feature = "pct_change")]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down

0 comments on commit 9ea46ef

Please sign in to comment.