Skip to content

Commit

Permalink
chore(rust): Move round to ops
Browse files Browse the repository at this point in the history
  • Loading branch information
reswqa committed Oct 18, 2023
1 parent d24c508 commit 9ea2f96
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
1 change: 0 additions & 1 deletion crates/polars-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ docs-selection = [
"temporal",
"random",
"zip_with",
"round_series",
"checked_arithmetic",
"is_first_distinct",
"is_last_distinct",
Expand Down
9 changes: 0 additions & 9 deletions crates/polars-core/src/series/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
2 changes: 0 additions & 2 deletions crates/polars-core/src/series/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,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")]
Expand Down
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 @@ -34,6 +34,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")]
Expand Down Expand Up @@ -77,6 +79,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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,84 @@
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<Self> {
if let Ok(ca) = self.f32() {
if decimals == 0 {
fn round(&self, decimals: u32) -> PolarsResult<Series> {
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
let multiplier = 10.0.pow(decimals as f64);
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<Self> {
if let Ok(ca) = self.f32() {
fn floor(&self) -> PolarsResult<Series> {
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<Self> {
if let Ok(ca) = self.f32() {
fn ceil(&self) -> PolarsResult<Series> {
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));
}
}
2 changes: 1 addition & 1 deletion crates/polars-plan/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down

0 comments on commit 9ea2f96

Please sign in to comment.