Skip to content

Commit

Permalink
chore(rust): Factor out ensure_is_constant_duration (#15733)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli authored Apr 18, 2024
1 parent 252e4cf commit 2e9674e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 1 addition & 7 deletions crates/polars-plan/src/dsl/function_expr/ewm_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ pub(super) fn ewm_mean_by(
_ => None,
};
polars_ensure!(!half_life.negative(), InvalidOperation: "half_life cannot be negative");
polars_ensure!(half_life.is_constant_duration(time_zone),
InvalidOperation: "expected `half_life` to be a constant duration \
(i.e. one independent of differing month durations or of daylight savings time), got {}.\n\
\n\
You may want to try:\n\
- using `'730h'` instead of `'1mo'`\n\
- using `'24h'` instead of `'1d'` if your series is time-zone-aware", half_life);
ensure_is_constant_duration(half_life, time_zone, "half_life")?;
// `half_life` is a constant duration so we can safely use `duration_ns()`.
let half_life = half_life.duration_ns();
let values = &s[0];
Expand Down
16 changes: 16 additions & 0 deletions crates/polars-time/src/windows/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use polars_core::prelude::{
datetime_to_timestamp_ms, datetime_to_timestamp_ns, datetime_to_timestamp_us, polars_bail,
PolarsResult,
};
use polars_error::polars_ensure;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -947,6 +948,21 @@ fn new_datetime(
Some(NaiveDateTime::new(date, time))
}

pub fn ensure_is_constant_duration(
duration: Duration,
time_zone: Option<&str>,
variable_name: &str,
) -> PolarsResult<()> {
polars_ensure!(duration.is_constant_duration(time_zone),
InvalidOperation: "expected `{}` to be a constant duration \
(i.e. one independent of differing month durations or of daylight savings time), got {}.\n\
\n\
You may want to try:\n\
- using `'730h'` instead of `'1mo'`\n\
- using `'24h'` instead of `'1d'` if your series is time-zone-aware", variable_name, duration);
Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 2e9674e

Please sign in to comment.