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

fix: `rolling_*_by was throwing incorrect error when dataframe was sorted by contained multiple chunks #16247

Merged
merged 1 commit into from
May 16, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ where
return Ok(Series::new_empty(ca.name(), ca.dtype()));
}
let ca = ca.rechunk();
let by = by.rechunk();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The error comes from L112

    let by_values = by.cont_slice().map_err(|_| {
        polars_err!(
            ComputeError:
            "`by` column should not have null values in 'rolling by' expression"
        )
    })?;

The values already get re-chunked, so the by ones probably should too?

ensure_duration_matches_data_type(options.window_size, by.dtype(), "window_size")?;
polars_ensure!(!options.window_size.is_zero() && !options.window_size.negative, InvalidOperation: "`window_size` must be strictly positive");
if by.is_sorted_flag() != IsSorted::Ascending && options.warn_if_unsorted {
Expand Down
13 changes: 13 additions & 0 deletions py-polars/tests/unit/operations/rolling/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,19 @@ def test_temporal_windows_size_without_by_15977() -> None:
df.select(pl.col("a").rolling_mean("3d"))


def test_incorrect_nulls_16246() -> None:
df = pl.concat(
[
pl.DataFrame({"a": [datetime(2020, 1, 1)], "b": [1]}),
pl.DataFrame({"a": [datetime(2021, 1, 1)], "b": [1]}),
],
rechunk=False,
)
result = df.select(pl.col("b").rolling_max_by("a", "1d"))
expected = pl.DataFrame({"b": [1, 1]})
assert_frame_equal(result, expected)


def interval_defs() -> SearchStrategy[ClosedInterval]:
closed: list[ClosedInterval] = ["left", "right", "both", "none"]
return st.sampled_from(closed)
Expand Down
Loading