polars.Expr.cummax#

Expr.cummax(*, reverse: bool = False) Self[source]#

Get an array with the cumulative max computed at every element.

Parameters:
reverse

Reverse the operation.

Examples

>>> df = pl.DataFrame({"a": [1, 2, 3, 4]})
>>> df.select(
...     [
...         pl.col("a").cummax(),
...         pl.col("a").cummax(reverse=True).alias("a_reverse"),
...     ]
... )
shape: (4, 2)
┌─────┬───────────┐
│ a   ┆ a_reverse │
│ --- ┆ ---       │
│ i64 ┆ i64       │
╞═════╪═══════════╡
│ 1   ┆ 4         │
│ 2   ┆ 4         │
│ 3   ┆ 4         │
│ 4   ┆ 4         │
└─────┴───────────┘

Null values are excluded, but can also be filled by calling forward_fill.

>>> df = pl.DataFrame({"values": [None, 10, None, 8, 9, None, 16, None]})
>>> df.with_columns(
...     [
...         pl.col("values").cummax().alias("value_cummax"),
...         pl.col("values")
...         .cummax()
...         .forward_fill()
...         .alias("value_cummax_all_filled"),
...     ]
... )
shape: (8, 3)
┌────────┬──────────────┬─────────────────────────┐
│ values ┆ value_cummax ┆ value_cummax_all_filled │
│ ---    ┆ ---          ┆ ---                     │
│ i64    ┆ i64          ┆ i64                     │
╞════════╪══════════════╪═════════════════════════╡
│ null   ┆ null         ┆ null                    │
│ 10     ┆ 10           ┆ 10                      │
│ null   ┆ null         ┆ 10                      │
│ 8      ┆ 10           ┆ 10                      │
│ 9      ┆ 10           ┆ 10                      │
│ null   ┆ null         ┆ 10                      │
│ 16     ┆ 16           ┆ 16                      │
│ null   ┆ null         ┆ 16                      │
└────────┴──────────────┴─────────────────────────┘