polars.Expr.cumsum#

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

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

Parameters:
reverse

Reverse the operation.

Notes

Dtypes in {Int8, UInt8, Int16, UInt16} are cast to Int64 before summing to prevent overflow issues.

Examples

>>> df = pl.DataFrame({"a": [1, 2, 3, 4]})
>>> df.select(
...     [
...         pl.col("a").cumsum(),
...         pl.col("a").cumsum(reverse=True).alias("a_reverse"),
...     ]
... )
shape: (4, 2)
┌─────┬───────────┐
│ a   ┆ a_reverse │
│ --- ┆ ---       │
│ i64 ┆ i64       │
╞═════╪═══════════╡
│ 1   ┆ 10        │
│ 3   ┆ 9         │
│ 6   ┆ 7         │
│ 10  ┆ 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").cumsum().alias("value_cumsum"),
...         pl.col("values")
...         .cumsum()
...         .forward_fill()
...         .alias("value_cumsum_all_filled"),
...     ]
... )
shape: (8, 3)
┌────────┬──────────────┬─────────────────────────┐
│ values ┆ value_cumsum ┆ value_cumsum_all_filled │
│ ---    ┆ ---          ┆ ---                     │
│ i64    ┆ i64          ┆ i64                     │
╞════════╪══════════════╪═════════════════════════╡
│ null   ┆ null         ┆ null                    │
│ 10     ┆ 10           ┆ 10                      │
│ null   ┆ null         ┆ 10                      │
│ 8      ┆ 18           ┆ 18                      │
│ 9      ┆ 27           ┆ 27                      │
│ null   ┆ null         ┆ 27                      │
│ 16     ┆ 43           ┆ 43                      │
│ null   ┆ null         ┆ 43                      │
└────────┴──────────────┴─────────────────────────┘