Skip to content

Commit

Permalink
fix(rust, python): Reused input series in rolling_apply should not be…
Browse files Browse the repository at this point in the history
… orderly
  • Loading branch information
reswqa committed Aug 23, 2023
1 parent bc166ce commit 63af56c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/polars-core/src/chunked_array/ops/rolling_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ mod inner_mod {
}
// ensure the length is correct
series_container._get_inner_mut().compute_len();

// reset flags as we reuse this container
series_container.clear_settings();
let s = f(&series_container);
let out = self.unpack_series_matching_type(&s)?;
builder.append_option(out.get(0));
Expand Down
14 changes: 14 additions & 0 deletions py-polars/tests/unit/operations/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,20 @@ def test_rolling_window_size_9160() -> None:
).to_list() == [1]


def test_rolling_apply_clear_reuse_series_state_10681() -> None:
df = pl.DataFrame({"a": [1, 1, 1, 1, 2, 2, 2, 2], "b": [0, 1, 11.0, 7, 4, 2, 3, 8]})
assert df.with_columns(
pl.col("b")
.rolling_apply(lambda s: s.min(), window_size=3, min_periods=2)
.over("a")
.alias("min")
).to_dict(False) == {
"a": [1, 1, 1, 1, 2, 2, 2, 2],
"b": [0.0, 1.0, 11.0, 7.0, 4.0, 2.0, 3.0, 8.0],
"min": [None, 0.0, 0.0, 1.0, None, 2.0, 2.0, 2.0],
}


def test_rolling_empty_window_9406() -> None:
datecol = pl.Series(
"d",
Expand Down

0 comments on commit 63af56c

Please sign in to comment.