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

feat: raise more informative error messages in rolling_* aggregations instead of panicking #15979

Merged
merged 3 commits into from
May 2, 2024

Conversation

MarcoGorelli
Copy link
Collaborator

@MarcoGorelli MarcoGorelli commented Apr 30, 2024

closes #15977

The validation of params is currently a bit messy, I'm suggesting to do, within rolling_agg:

if by.is_none() {
    validate fixed window params
    rolling_fn
} else {
    validate dynamic window params
    rolling_fn_by
}

This will also make it easier to split out rolling_* from rolling_*_by

@github-actions github-actions bot added enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars labels Apr 30, 2024
impl TryFrom<RollingOptions> for RollingOptionsImpl<'static> {
type Error = PolarsError;

fn try_from(options: RollingOptions) -> PolarsResult<Self> {
let window_size = options.window_size;
assert!(
window_size.parsed_int,
"should be fixed integer window size at this point"
);
polars_ensure!(
options.closed_window.is_none(),
InvalidOperation: "`closed_window` is not supported for fixed window size rolling aggregations, \
consider using DataFrame.rolling for greater flexibility",
);

Ok(RollingOptionsImpl {
window_size,
impl From<RollingOptions> for RollingOptionsImpl<'static> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this from, instead of try_from, so the validation is done when going from RollingOptionsImpl to RollingOptionsFixedWindow (or to RollingOptionsDynamicWindow)

Copy link

codecov bot commented May 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.91%. Comparing base (414e5f6) to head (e06abce).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15979      +/-   ##
==========================================
+ Coverage   80.90%   80.91%   +0.01%     
==========================================
  Files        1384     1384              
  Lines      178196   178195       -1     
  Branches     3050     3050              
==========================================
+ Hits       144173   144191      +18     
+ Misses      33535    33518      -17     
+ Partials      488      486       -2     
Flag Coverage Δ
python 74.39% <100.00%> (+0.01%) ⬆️
rust 78.12% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

let arr = if options.window_size.parsed_int {
let arr = if options.by.is_none() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than branching based on window_size.parsed_int (which can lead to some surprising error messages), I think it's cleaner to branch on by.is_none

@MarcoGorelli MarcoGorelli marked this pull request as ready for review May 1, 2024 10:41
@@ -130,39 +126,39 @@ fn convert<'a>(
}

pub(super) fn rolling_min(s: &Series, options: RollingOptions) -> PolarsResult<Series> {
s.rolling_min(options.clone().try_into()?)
s.rolling_min(options.clone().into())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we clone here? We have town the options.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no reason as far as I can tell, I've removed it

@ritchie46 ritchie46 merged commit 864e750 into pola-rs:main May 2, 2024
25 checks passed
AlexanderNenninger pushed a commit to AlexanderNenninger/polars that referenced this pull request May 3, 2024
AlexanderNenninger pushed a commit to AlexanderNenninger/polars that referenced this pull request May 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or an improvement of an existing feature python Related to Python Polars rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecesary panic when rolling aggregations are performed with temporal window_size and no by
2 participants