Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rust): Factor out ensure_is_constant_duration #15733

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading