From 28a99f6336367246d33e7a8b82cd5e609256e68f Mon Sep 17 00:00:00 2001 From: Weijie Guo Date: Thu, 19 Oct 2023 11:36:35 +0800 Subject: [PATCH] chore(rust): Move round to ops (#11838) --- crates/polars-core/Cargo.toml | 1 - crates/polars-core/src/series/mod.rs | 9 --- crates/polars-core/src/series/ops/mod.rs | 2 - crates/polars-ops/src/series/ops/mod.rs | 4 ++ .../src/series/ops/round.rs | 66 ++++++++++++------- crates/polars-plan/Cargo.toml | 2 +- crates/polars/Cargo.toml | 2 +- 7 files changed, 50 insertions(+), 36 deletions(-) rename crates/{polars-core => polars-ops}/src/series/ops/round.rs (55%) diff --git a/crates/polars-core/Cargo.toml b/crates/polars-core/Cargo.toml index 12a8a536ebdd..8ec0afe8a936 100644 --- a/crates/polars-core/Cargo.toml +++ b/crates/polars-core/Cargo.toml @@ -138,7 +138,6 @@ docs-selection = [ "temporal", "random", "zip_with", - "round_series", "checked_arithmetic", "is_first_distinct", "is_last_distinct", diff --git a/crates/polars-core/src/series/mod.rs b/crates/polars-core/src/series/mod.rs index d8672a93ae56..5b978a29ce3c 100644 --- a/crates/polars-core/src/series/mod.rs +++ b/crates/polars-core/src/series/mod.rs @@ -1043,13 +1043,4 @@ mod test { let _ = series.slice(-6, 2); let _ = series.slice(4, 2); } - - #[test] - #[cfg(feature = "round_series")] - fn test_round_series() { - let series = Series::new("a", &[1.003, 2.23222, 3.4352]); - let out = series.round(2).unwrap(); - let ca = out.f64().unwrap(); - assert_eq!(ca.get(0), Some(1.0)); - } } diff --git a/crates/polars-core/src/series/ops/mod.rs b/crates/polars-core/src/series/ops/mod.rs index 48766748f58b..650e5cbecaa4 100644 --- a/crates/polars-core/src/series/ops/mod.rs +++ b/crates/polars-core/src/series/ops/mod.rs @@ -3,8 +3,6 @@ mod extend; #[cfg(feature = "moment")] pub mod moment; mod null; -#[cfg(feature = "round_series")] -mod round; mod to_list; mod unique; #[cfg(feature = "serde")] diff --git a/crates/polars-ops/src/series/ops/mod.rs b/crates/polars-ops/src/series/ops/mod.rs index b771520bd192..ea7f4a9a455e 100644 --- a/crates/polars-ops/src/series/ops/mod.rs +++ b/crates/polars-ops/src/series/ops/mod.rs @@ -36,6 +36,8 @@ mod rank; mod rle; #[cfg(feature = "rolling_window")] mod rolling; +#[cfg(feature = "round_series")] +mod round; #[cfg(feature = "search_sorted")] mod search_sorted; #[cfg(feature = "to_dummies")] @@ -81,6 +83,8 @@ pub use rank::*; pub use rle::*; #[cfg(feature = "rolling_window")] pub use rolling::*; +#[cfg(feature = "round_series")] +pub use round::*; #[cfg(feature = "search_sorted")] pub use search_sorted::*; #[cfg(feature = "to_dummies")] diff --git a/crates/polars-core/src/series/ops/round.rs b/crates/polars-ops/src/series/ops/round.rs similarity index 55% rename from crates/polars-core/src/series/ops/round.rs rename to crates/polars-ops/src/series/ops/round.rs index 37abe7797941..542107ed5871 100644 --- a/crates/polars-core/src/series/ops/round.rs +++ b/crates/polars-ops/src/series/ops/round.rs @@ -1,14 +1,17 @@ use num_traits::pow::Pow; +use polars_core::prelude::*; -use crate::prelude::*; +use crate::series::ops::SeriesSealed; -impl Series { +pub trait RoundSeries: SeriesSealed { /// Round underlying floating point array to given decimal. - pub fn round(&self, decimals: u32) -> PolarsResult { - if let Ok(ca) = self.f32() { - if decimals == 0 { + fn round(&self, decimals: u32) -> PolarsResult { + let s = self.as_series(); + + if let Ok(ca) = s.f32() { + return if decimals == 0 { let s = ca.apply_values(|val| val.round()).into_series(); - return Ok(s); + Ok(s) } else { // Note we do the computation on f64 floats to not lose precision // when the computation is done, we cast to f32 @@ -16,47 +19,66 @@ impl Series { let s = ca .apply_values(|val| ((val as f64 * multiplier).round() / multiplier) as f32) .into_series(); - return Ok(s); - } + Ok(s) + }; } - if let Ok(ca) = self.f64() { - if decimals == 0 { + if let Ok(ca) = s.f64() { + return if decimals == 0 { let s = ca.apply_values(|val| val.round()).into_series(); - return Ok(s); + Ok(s) } else { let multiplier = 10.0.pow(decimals as f64); let s = ca .apply_values(|val| (val * multiplier).round() / multiplier) .into_series(); - return Ok(s); - } + Ok(s) + }; } - polars_bail!(opq = round, self.dtype()); + polars_bail!(opq = round, s.dtype()); } /// Floor underlying floating point array to the lowest integers smaller or equal to the float value. - pub fn floor(&self) -> PolarsResult { - if let Ok(ca) = self.f32() { + fn floor(&self) -> PolarsResult { + let s = self.as_series(); + + if let Ok(ca) = s.f32() { let s = ca.apply_values(|val| val.floor()).into_series(); return Ok(s); } - if let Ok(ca) = self.f64() { + if let Ok(ca) = s.f64() { let s = ca.apply_values(|val| val.floor()).into_series(); return Ok(s); } - polars_bail!(opq = floor, self.dtype()); + polars_bail!(opq = floor, s.dtype()); } /// Ceil underlying floating point array to the highest integers smaller or equal to the float value. - pub fn ceil(&self) -> PolarsResult { - if let Ok(ca) = self.f32() { + fn ceil(&self) -> PolarsResult { + let s = self.as_series(); + + if let Ok(ca) = s.f32() { let s = ca.apply_values(|val| val.ceil()).into_series(); return Ok(s); } - if let Ok(ca) = self.f64() { + if let Ok(ca) = s.f64() { let s = ca.apply_values(|val| val.ceil()).into_series(); return Ok(s); } - polars_bail!(opq = ceil, self.dtype()); + polars_bail!(opq = ceil, s.dtype()); + } +} + +impl RoundSeries for Series {} + +#[cfg(test)] +mod test { + use crate::prelude::*; + + #[test] + fn test_round_series() { + let series = Series::new("a", &[1.003, 2.23222, 3.4352]); + let out = series.round(2).unwrap(); + let ca = out.f64().unwrap(); + assert_eq!(ca.get(0), Some(1.0)); } } diff --git a/crates/polars-plan/Cargo.toml b/crates/polars-plan/Cargo.toml index 99fbd668c782..1a088bc806ef 100644 --- a/crates/polars-plan/Cargo.toml +++ b/crates/polars-plan/Cargo.toml @@ -89,7 +89,7 @@ extract_jsonpath = ["polars-ops/extract_jsonpath"] approx_unique = ["polars-ops/approx_unique"] is_in = ["polars-ops/is_in"] repeat_by = ["polars-ops/repeat_by"] -round_series = ["polars-core/round_series"] +round_series = ["polars-ops/round_series"] is_first_distinct = ["polars-core/is_first_distinct", "polars-ops/is_first_distinct"] is_last_distinct = ["polars-core/is_last_distinct", "polars-ops/is_last_distinct"] is_unique = ["polars-ops/is_unique"] diff --git a/crates/polars/Cargo.toml b/crates/polars/Cargo.toml index 3ca2884540ca..369b1d07f9cf 100644 --- a/crates/polars/Cargo.toml +++ b/crates/polars/Cargo.toml @@ -113,7 +113,7 @@ sort_multiple = ["polars-core/sort_multiple"] approx_unique = ["polars-lazy?/approx_unique", "polars-ops/approx_unique"] is_in = ["polars-lazy?/is_in"] zip_with = ["polars-core/zip_with", "polars-ops/zip_with"] -round_series = ["polars-core/round_series", "polars-lazy?/round_series", "polars-ops/round_series"] +round_series = ["polars-ops/round_series", "polars-lazy?/round_series"] checked_arithmetic = ["polars-core/checked_arithmetic"] repeat_by = ["polars-ops/repeat_by", "polars-lazy?/repeat_by"] is_first_distinct = ["polars-lazy?/is_first_distinct", "polars-ops/is_first_distinct"]