Skip to content

Cumulative product

Source code

Description

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

Usage

<Expr>$cum_prod(reverse = FALSE)

Arguments

reverse If TRUE, start with the total product of elements and divide each row one by one.

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 = 1:4)$with_columns(
  pl$col("a")$cum_prod()$alias("cum_prod"),
  pl$col("a")$cum_prod(reverse = TRUE)$alias("cum_prod_reversed")
)
#> shape: (4, 3)
#> ┌─────┬──────────┬───────────────────┐
#> │ a   ┆ cum_prod ┆ cum_prod_reversed │
#> │ --- ┆ ---      ┆ ---               │
#> │ i32 ┆ i64      ┆ i64               │
#> ╞═════╪══════════╪═══════════════════╡
#> │ 1   ┆ 1        ┆ 24                │
#> │ 2   ┆ 2        ┆ 24                │
#> │ 3   ┆ 6        ┆ 12                │
#> │ 4   ┆ 24       ┆ 4                 │
#> └─────┴──────────┴───────────────────┘