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_* aggs were behaving as if they return scalars in group-by #15657

Merged
merged 1 commit into from
Apr 15, 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
2 changes: 1 addition & 1 deletion crates/polars-plan/src/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ impl Expr {
self.apply_many_private(
FunctionExpr::RollingExpr(rolling_function_by(options)),
&[col(&name)],
true,
false,
Copy link
Member

Choose a reason for hiding this comment

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

Ai, good catch. That took a long time.

Copy link
Collaborator

@reswqa reswqa Apr 15, 2024

Choose a reason for hiding this comment

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

Just wondering what's the principle here, it seems to be the same behavior for reduce/fold (return_scalars=True).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Rolling functions return something which is the same length as the input, so I don't think they should ever return a scalar

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeap, I see. But IIRC, the output of fold/reduce should also be consistent with the length of the input.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ah thanks - will take a look, have made a note-to-self

false,
)
} else {
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 @@ -917,3 +917,16 @@ def test_rolling_min_periods(
)
)["value"]
assert_series_equal(result, pl.Series("value", expected, pl.Int64))


def test_rolling_returns_scalar_15656() -> None:
df = pl.DataFrame(
{
"a": [date(2020, 1, 1), date(2020, 1, 2), date(2020, 1, 3)],
"b": [4, 5, 6],
"c": [1, 2, 3],
}
)
result = df.group_by("c").agg(pl.col("b").rolling_mean("2d", by="a")).sort("c")
expected = pl.DataFrame({"c": [1, 2, 3], "b": [[4.0], [5.0], [6.0]]})
assert_frame_equal(result, expected)
Loading