Skip to content

Cumulative maximum

Source code

Description

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

Usage

<Expr>$cum_max(reverse = FALSE)

Arguments

reverse If TRUE, start from the last value.

Details

The Dtypes Int8, UInt8, Int16 and UInt16 are cast to Int64 before summing to prevent overflow issues.

Value

Expr

Examples

library(polars)

pl$DataFrame(a = c(1:4, 2L))$with_columns(
  pl$col("a")$cum_max()$alias("cummux"),
  pl$col("a")$cum_max(reverse = TRUE)$alias("cum_max_reversed")
)
#> shape: (5, 3)
#> ┌─────┬────────┬──────────────────┐
#> │ a   ┆ cummux ┆ cum_max_reversed │
#> │ --- ┆ ---    ┆ ---              │
#> │ i32 ┆ i32    ┆ i32              │
#> ╞═════╪════════╪══════════════════╡
#> │ 1   ┆ 1      ┆ 4                │
#> │ 2   ┆ 2      ┆ 4                │
#> │ 3   ┆ 3      ┆ 4                │
#> │ 4   ┆ 4      ┆ 4                │
#> │ 2   ┆ 4      ┆ 2                │
#> └─────┴────────┴──────────────────┘