Skip to content

Difference

Source code

Description

Calculate the n-th discrete difference.

Usage

<Expr>$diff(n = 1, null_behavior = c("ignore", "drop"))

Arguments

n Number of slots to shift.
null_behavior String, either “ignore” (default), else “drop”.

Value

Expr

Examples

library("polars")

pl$DataFrame(a = c(20L, 10L, 30L, 40L))$with_columns(
  diff_default = pl$col("a")$diff(),
  diff_2_ignore = pl$col("a")$diff(2, "ignore")
)
#> shape: (4, 3)
#> ┌─────┬──────────────┬───────────────┐
#> │ a   ┆ diff_default ┆ diff_2_ignore │
#> │ --- ┆ ---          ┆ ---           │
#> │ i32 ┆ i32          ┆ i32           │
#> ╞═════╪══════════════╪═══════════════╡
#> │ 20  ┆ null         ┆ null          │
#> │ 10  ┆ -10          ┆ null          │
#> │ 30  ┆ 20           ┆ 10            │
#> │ 40  ┆ 10           ┆ 30            │
#> └─────┴──────────────┴───────────────┘