Skip to content

Commit

Permalink
chore(rust): move abs to ops
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed Oct 24, 2023
1 parent 7c83592 commit 717d123
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 43 deletions.
2 changes: 0 additions & 2 deletions crates/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ group_by_list = []
rolling_window = []
diagonal_concat = []
horizontal_concat = []
abs = []
dataframe_arithmetic = []
product = []
unique_counts = []
Expand Down Expand Up @@ -148,7 +147,6 @@ docs-selection = [
"dtype-decimal",
"diagonal_concat",
"horizontal_concat",
"abs",
"dataframe_arithmetic",
"product",
"unique_counts",
Expand Down
14 changes: 0 additions & 14 deletions crates/polars-core/src/chunked_array/ops/abs.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/polars-core/src/chunked_array/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use arrow::offset::OffsetsBuffer;
use crate::datatypes::ObjectType;
use crate::prelude::*;

#[cfg(feature = "abs")]
mod abs;
pub(crate) mod aggregate;
pub(crate) mod any_value;
pub(crate) mod append;
Expand Down
20 changes: 0 additions & 20 deletions crates/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,26 +765,6 @@ impl Series {
}
}

#[cfg(feature = "abs")]
/// convert numerical values to their absolute value
pub fn abs(&self) -> PolarsResult<Series> {
let a = self.to_physical_repr();
use DataType::*;
let out = match a.dtype() {
#[cfg(feature = "dtype-i8")]
Int8 => a.i8().unwrap().abs().into_series(),
#[cfg(feature = "dtype-i16")]
Int16 => a.i16().unwrap().abs().into_series(),
Int32 => a.i32().unwrap().abs().into_series(),
Int64 => a.i64().unwrap().abs().into_series(),
UInt8 | UInt16 | UInt32 | UInt64 => self.clone(),
Float32 => a.f32().unwrap().abs().into_series(),
Float64 => a.f64().unwrap().abs().into_series(),
dt => polars_bail!(opq = abs, dt),
};
out.cast(self.dtype())
}

// used for formatting
pub fn str_value(&self, index: usize) -> PolarsResult<Cow<str>> {
let out = match self.0.get(index)? {
Expand Down
1 change: 1 addition & 0 deletions crates/polars-ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,4 @@ repeat_by = []
peaks = []
cum_agg = []
ewma = []
abs = []
29 changes: 29 additions & 0 deletions crates/polars-ops/src/series/ops/abs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use num_traits::Signed;
use polars_core::prelude::*;

fn abs_numeric<T>(ca: &ChunkedArray<T>) -> ChunkedArray<T>
where
T: PolarsNumericType,
T::Native: Signed,
{
ca.apply_values(|v| v.abs())
}

/// convert numerical values to their absolute value
pub fn abs(s: &Series) -> PolarsResult<Series> {
let physical_s = s.to_physical_repr();
use DataType::*;
let out = match physical_s.dtype() {
#[cfg(feature = "dtype-i8")]
Int8 => abs_numeric(physical_s.i8()?).into_series(),
#[cfg(feature = "dtype-i16")]
Int16 => abs_numeric(physical_s.i16()?).into_series(),
Int32 => abs_numeric(physical_s.i32()?).into_series(),
Int64 => abs_numeric(physical_s.i64()?).into_series(),
UInt8 | UInt16 | UInt32 | UInt64 => s.clone(),
Float32 => abs_numeric(physical_s.f32()?).into_series(),
Float64 => abs_numeric(physical_s.f64()?).into_series(),
dt => polars_bail!(opq = abs, dt),
};
out.cast(s.dtype())
}
4 changes: 4 additions & 0 deletions crates/polars-ops/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "abs")]
mod abs;
mod approx_algo;
#[cfg(feature = "approx_unique")]
mod approx_unique;
Expand Down Expand Up @@ -46,6 +48,8 @@ mod search_sorted;
mod to_dummies;
mod various;

#[cfg(feature = "abs")]
pub use abs::*;
pub use approx_algo::*;
#[cfg(feature = "approx_unique")]
pub use approx_unique::*;
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 @@ -110,7 +110,7 @@ rank = ["polars-ops/rank"]
diff = ["polars-ops/diff"]
pct_change = ["polars-ops/pct_change"]
moment = ["polars-ops/moment"]
abs = ["polars-core/abs"]
abs = ["polars-ops/abs"]
random = ["polars-core/random"]
dynamic_group_by = ["polars-core/dynamic_group_by"]
ewma = ["polars-ops/ewma"]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/function_expr/abs.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::*;

pub(super) fn abs(s: &Series) -> PolarsResult<Series> {
s.abs()
polars_ops::prelude::abs(s)
}
5 changes: 3 additions & 2 deletions crates/polars-time/src/group_by/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@ fn update_subgroups_idx(
#[cfg(test)]
mod test {
use chrono::prelude::*;
use polars_ops::prelude::abs;

use super::*;

Expand Down Expand Up @@ -777,11 +778,11 @@ mod test {
"",
[0.0, 8.0, 4.000000000000002, 6.666666666666667, 24.5, 0.0],
);
assert!((var - expected).abs().unwrap().lt(1e-12).unwrap().all());
assert!(abs(&(var - expected)).unwrap().lt(1e-12).unwrap().all());

let var = unsafe { nulls.agg_var(&groups, 1) };
let expected = Series::new("", [0.0, 8.0, 8.0, 9.333333333333343, 24.5, 0.0]);
assert!((var - expected).abs().unwrap().lt(1e-12).unwrap().all());
assert!(abs(&(var - expected)).unwrap().lt(1e-12).unwrap().all());

let quantile = unsafe { a.agg_quantile(&groups, 0.5, QuantileInterpolOptions::Linear) };
let expected = Series::new("", [3.0, 5.0, 5.0, 6.0, 5.5, 1.0]);
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ range = ["polars-lazy?/range"]
true_div = ["polars-lazy?/true_div"]
diagonal_concat = ["polars-core/diagonal_concat", "polars-lazy?/diagonal_concat", "polars-sql?/diagonal_concat"]
horizontal_concat = ["polars-core/horizontal_concat"]
abs = ["polars-core/abs", "polars-lazy?/abs"]
abs = ["polars-ops/abs", "polars-lazy?/abs"]
dynamic_group_by = ["polars-core/dynamic_group_by", "polars-lazy?/dynamic_group_by"]
ewma = ["polars-ops/ewma", "polars-lazy?/ewma"]
dot_diagram = ["polars-lazy?/dot_diagram"]
Expand Down

0 comments on commit 717d123

Please sign in to comment.