polars.Expr.over#

Expr.over(expr: str | polars.internals.expr.expr.Expr | list[polars.internals.expr.expr.Expr | str]) Expr[source]#

Apply window function over a subgroup.

This is similar to a groupby + aggregation + self join. Or similar to window functions in Postgres.

Parameters:
expr

Column(s) to group by.

Examples

>>> df = pl.DataFrame(
...     {
...         "groups": ["g1", "g1", "g2"],
...         "values": [1, 2, 3],
...     }
... )
>>> (
...     df.with_column(
...         pl.col("values").max().over("groups").alias("max_by_group")
...     )
... )
shape: (3, 3)
┌────────┬────────┬──────────────┐
│ groups ┆ values ┆ max_by_group │
│ ---    ┆ ---    ┆ ---          │
│ str    ┆ i64    ┆ i64          │
╞════════╪════════╪══════════════╡
│ g1     ┆ 1      ┆ 2            │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ g1     ┆ 2      ┆ 2            │
├╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
│ g2     ┆ 3      ┆ 3            │
└────────┴────────┴──────────────┘
>>> df = pl.DataFrame(
...     {
...         "groups": [1, 1, 2, 2, 1, 2, 3, 3, 1],
...         "values": [1, 2, 3, 4, 5, 6, 7, 8, 8],
...     }
... )
>>> (
...     df.lazy()
...     .select(
...         [
...             pl.col("groups").sum().over("groups"),
...         ]
...     )
...     .collect()
... )
shape: (9, 1)
┌────────┐
│ groups │
│ ---    │
│ i64    │
╞════════╡
│ 4      │
├╌╌╌╌╌╌╌╌┤
│ 4      │
├╌╌╌╌╌╌╌╌┤
│ 6      │
├╌╌╌╌╌╌╌╌┤
│ 6      │
├╌╌╌╌╌╌╌╌┤
│ ...    │
├╌╌╌╌╌╌╌╌┤
│ 6      │
├╌╌╌╌╌╌╌╌┤
│ 6      │
├╌╌╌╌╌╌╌╌┤
│ 6      │
├╌╌╌╌╌╌╌╌┤
│ 4      │
└────────┘