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(python, rust): enforce sortedness of by argument in rolling_* functions #11002

Merged
merged 3 commits into from
Sep 14, 2023
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
3 changes: 3 additions & 0 deletions crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(ambiguous_glob_reexports)]
//! Domain specific language for the Lazy API.
#[cfg(feature = "rolling_window")]
use polars_core::utils::ensure_sorted_arg;
#[cfg(feature = "dtype-categorical")]
pub mod cat;
#[cfg(feature = "dtype-categorical")]
Expand Down Expand Up @@ -1239,6 +1241,7 @@ impl Expr {
},
_ => (by.clone(), &None),
};
ensure_sorted_arg(&by, expr_name)?;
let by = by.datetime().unwrap();
let by_values = by.cont_slice().map_err(|_| {
polars_err!(
Expand Down
20 changes: 17 additions & 3 deletions py-polars/tests/unit/operations/rolling/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def example_df() -> pl.DataFrame:
def test_rolling_kernels_and_group_by_rolling(
example_df: pl.DataFrame, period: str | timedelta, closed: ClosedInterval
) -> None:
out1 = example_df.select(
out1 = example_df.set_sorted("dt").select(
[
pl.col("dt"),
# this differs from group_by aggregation because the empty window is
Expand Down Expand Up @@ -790,7 +790,7 @@ def test_rolling_empty_window_9406(time_unit: TimeUnit) -> None:
"d",
[datetime(2019, 1, x) for x in [16, 17, 18, 22, 23]],
dtype=pl.Datetime(time_unit=time_unit, time_zone=None),
)
).set_sorted()
rawdata = pl.Series("x", [1.1, 1.2, 1.3, 1.15, 1.25], dtype=pl.Float64)
rmin = pl.Series("x", [None, 1.1, 1.1, None, 1.15], dtype=pl.Float64)
rmax = pl.Series("x", [None, 1.1, 1.2, None, 1.15], dtype=pl.Float64)
Expand Down Expand Up @@ -835,6 +835,20 @@ def test_rolling_weighted_quantile_10031() -> None:
)


def test_rolling_aggregations_unsorted_raise_10991() -> None:
df = pl.DataFrame(
{
"dt": [datetime(2020, 1, 3), datetime(2020, 1, 1), datetime(2020, 1, 2)],
"val": [1, 2, 3],
}
)
with pytest.raises(
pl.InvalidOperationError,
match="argument in operation 'rolling_sum' is not explicitly sorted",
):
df.with_columns(roll=pl.col("val").rolling_sum("2d", by="dt", closed="right"))


def test_rolling() -> None:
a = pl.Series("a", [1, 2, 3, 2, 1])
assert_series_equal(a.rolling_min(2), pl.Series("a", [None, 1, 2, 2, 1]))
Expand Down Expand Up @@ -914,7 +928,7 @@ def test_rolling_nanoseconds_11003() -> None:
"val": [1, 2, 3],
}
)
df = df.with_columns(pl.col("dt").str.to_datetime(time_unit="ns"))
df = df.with_columns(pl.col("dt").str.to_datetime(time_unit="ns")).set_sorted("dt")
result = df.with_columns(
pl.col("val").rolling_sum("500ns", by="dt", closed="right")
)
Expand Down