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

docs(python): Added example for explode mapping strategy in pl.Expr.over #15402

Merged
Merged
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
27 changes: 25 additions & 2 deletions py-polars/polars/expr/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3245,8 +3245,11 @@ def over(
Join the groups as 'List<group_dtype>' to the row positions.
warning: this can be memory intensive.
- explode
Don't do any mapping, but simply flatten the group.
This only makes sense if the input data is sorted.
Explodes the grouped data into new rows, similar to the results of
`group_by` + `agg` + `explode`. Sorting of the given groups is required
if the groups are not part of the window operation for the operation,
otherwise the result would not make sense. This operation changes the
number of rows.

Examples
--------
Expand Down Expand Up @@ -3328,6 +3331,26 @@ def over(
│ b ┆ 5 ┆ 2 ┆ 1 │
│ b ┆ 3 ┆ 1 ┆ 1 │
└─────┴─────┴─────┴───────┘

Aggregate values from each group using `mapping_strategy="explode"`.

>>> df.select(
... pl.col("a").head(2).over("a", mapping_strategy="explode"),
... pl.col("b").sort_by("b").head(2).over("a", mapping_strategy="explode"),
... pl.col("c").sort_by("b").head(2).over("a", mapping_strategy="explode"),
... )
shape: (4, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ a ┆ 1 ┆ 5 │
│ a ┆ 2 ┆ 4 │
│ b ┆ 3 ┆ 3 │
│ b ┆ 3 ┆ 1 │
└─────┴─────┴─────┘

"""
exprs = parse_as_list_of_expressions(expr, *more_exprs)
return self._from_pyexpr(self._pyexpr.over(exprs, mapping_strategy))
Expand Down
Loading