Skip to content

Percentage change

Source code

Description

Computes percentage change (as fraction) between current element and most- recent non-null element at least n period(s) before the current element. Computes the change from the previous row by default.

Usage

<Expr>$pct_change(n = 1)

Arguments

n Periods to shift for computing percent change.

Value

Expr

Examples

library(polars)

pl$DataFrame(a = c(10L, 11L, 12L, NA_integer_, 12L))$
  with_columns(pct_change = pl$col("a")$pct_change())
#> shape: (5, 2)
#> ┌──────┬────────────┐
#> │ a    ┆ pct_change │
#> │ ---  ┆ ---        │
#> │ i32  ┆ f64        │
#> ╞══════╪════════════╡
#> │ 10   ┆ null       │
#> │ 11   ┆ 0.1        │
#> │ 12   ┆ 0.090909   │
#> │ null ┆ 0.0        │
#> │ 12   ┆ 0.0        │
#> └──────┴────────────┘