Skip to content

Cumulatively sum all values

Source code

Description

This function is syntactic sugar for col(names)$cum_sum().

Usage

pl$cum_sum(names, ...)

Arguments

names The name(s) or data type of the column(s) to use in the aggregation. A character vector or a Polars data type/list of data types can be supplied.
These dots are for future extensions and must be empty.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = c(1, 8, 3),
  b = c(4, 5, 2),
  c = c("foo", "bar", "foo")
)

# Get the cum_sum of a column
df$select(pl$cum_sum("a"))
#> shape: (3, 1)
#> ┌──────┐
#> │ a    │
#> │ ---  │
#> │ f64  │
#> ╞══════╡
#> │ 1.0  │
#> │ 9.0  │
#> │ 12.0 │
#> └──────┘
# Get the cum_sum of multiple columns
df$select(pl$cum_sum(c("a", "b")))
#> shape: (3, 2)
#> ┌──────┬──────┐
#> │ a    ┆ b    │
#> │ ---  ┆ ---  │
#> │ f64  ┆ f64  │
#> ╞══════╪══════╡
#> │ 1.0  ┆ 4.0  │
#> │ 9.0  ┆ 9.0  │
#> │ 12.0 ┆ 11.0 │
#> └──────┴──────┘