Skip to content

Shift values by the given number of indices

Source code

Description

Shift values by the given number of indices

Usage

<Expr>$shift(n = 1, fill_value = NULL)

Arguments

n Number of indices to shift forward. If a negative value is passed, values are shifted in the opposite direction instead.
fill_value Fill the resulting null values with this value. Accepts expression input. Non-expression inputs are parsed as literals.

Value

Expr

Examples

library("polars")

pl$DataFrame(a = c(1, 2, 4, 5, 8))$
  with_columns(
  pl$col("a")$shift(-2)$alias("shift-2"),
  pl$col("a")$shift(2)$alias("shift+2")
)
#> shape: (5, 3)
#> ┌─────┬─────────┬─────────┐
#> │ a   ┆ shift-2 ┆ shift+2 │
#> │ --- ┆ ---     ┆ ---     │
#> │ f64 ┆ f64     ┆ f64     │
#> ╞═════╪═════════╪═════════╡
#> │ 1.0 ┆ 4.0     ┆ null    │
#> │ 2.0 ┆ 5.0     ┆ null    │
#> │ 4.0 ┆ 8.0     ┆ 1.0     │
#> │ 5.0 ┆ null    ┆ 2.0     │
#> │ 8.0 ┆ null    ┆ 4.0     │
#> └─────┴─────────┴─────────┘