Skip to content

Commit

Permalink
feat: raise informative error for rolling_* aggs with by of invalid…
Browse files Browse the repository at this point in the history
… dtype (#15088)
  • Loading branch information
MarcoGorelli authored Mar 18, 2024
1 parent 1195f85 commit 69be379
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
6 changes: 5 additions & 1 deletion crates/polars-plan/src/dsl/function_expr/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ fn convert<'a>(
by.cast(&DataType::Datetime(TimeUnit::Milliseconds, None))?,
&None,
),
dt => polars_bail!(opq = expr_name, got = dt, expected = "date/datetime"),
dt => polars_bail!(InvalidOperation:
"in `{}` operation, `by` argument of dtype `{}` is not supported (expected `{}`)",
expr_name,
dt,
"date/datetime"),
};
if by.is_sorted_flag() != IsSorted::Ascending && options.warn_if_unsorted {
polars_warn!(format!(
Expand Down
8 changes: 7 additions & 1 deletion py-polars/tests/unit/operations/rolling/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from numpy import nan

import polars as pl
from polars.exceptions import ComputeError
from polars.exceptions import ComputeError, InvalidOperationError
from polars.testing import assert_frame_equal, assert_series_equal

if TYPE_CHECKING:
Expand Down Expand Up @@ -217,6 +217,12 @@ def test_rolling_crossing_dst(
assert_frame_equal(result, expected)


def test_rolling_by_invalid() -> None:
df = pl.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]}).sort("a")
with pytest.raises(InvalidOperationError, match="`rolling_min` operation"):
df.select(pl.col("b").rolling_min(2, by="a"))


def test_rolling_infinity() -> None:
s = pl.Series("col", ["-inf", "5", "5"]).cast(pl.Float64)
s = s.rolling_mean(2)
Expand Down

0 comments on commit 69be379

Please sign in to comment.