Skip to content

Commit

Permalink
don't fully cast array in round_sig_figs
Browse files Browse the repository at this point in the history
  • Loading branch information
orlp committed Nov 9, 2023
1 parent 050ba19 commit 11c9ac9
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions crates/polars-ops/src/series/ops/round.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,17 @@ pub trait RoundSeries: SeriesSealed {

fn round_sig_figs(&self, digits: i32) -> PolarsResult<Series> {
let s = self.as_series();
if digits < 1 {
polars_bail!(
InvalidOperation: "digits must be an integer >= 1"
)
};

polars_ensure!(digits >= 1, InvalidOperation: "digits must be an integer >= 1");
polars_ensure!(s.dtype().is_numeric(), InvalidOperation: "round_sig_figs can only be used on numeric types" );
with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let s = s.cast(&DataType::Float64)?;
let ca = s.f64()?;
let ca: &ChunkedArray<$T> = s.as_ref().as_ref().as_ref();
let s = ca.apply_values(|value| {
let value = value as f64;
if value == 0.0 {
return 0.0;
return value as <$T as PolarsNumericType>::Native;
}
let magnitude = 10.0_f64.powi(digits - 1 - ((value.abs()).log10().floor() as i32));
(value * magnitude).round() / magnitude
let magnitude = 10.0_f64.powi(digits - 1 - value.abs().log10().floor() as i32);
((value * magnitude).round() / magnitude) as <$T as PolarsNumericType>::Native
}).into_series();
return Ok(s);
});
Expand Down

0 comments on commit 11c9ac9

Please sign in to comment.