Skip to content

Shift array values by n indices

Source code

Description

Shift array values by n indices

Usage

<Expr>$arr$shift(periods = 1)

Arguments

periods Number of places to shift (may be negative). Can be an Expr. Strings are not parsed as columns.

Value

Expr

Examples

library(polars)

df = pl$DataFrame(
  values = list(1:3, c(2L, NA_integer_, 5L)),
  idx = 1:2,
  schema = list(values = pl$Array(pl$Int32, 3))
)
df$with_columns(
  shift_by_expr = pl$col("values")$arr$shift(pl$col("idx")),
  shift_by_lit = pl$col("values")$arr$shift(2)
)
#> shape: (2, 4)
#> ┌───────────────┬─────┬─────────────────┬─────────────────┐
#> │ values        ┆ idx ┆ shift_by_expr   ┆ shift_by_lit    │
#> │ ---           ┆ --- ┆ ---             ┆ ---             │
#> │ array[i32, 3] ┆ i32 ┆ array[i32, 3]   ┆ array[i32, 3]   │
#> ╞═══════════════╪═════╪═════════════════╪═════════════════╡
#> │ [1, 2, 3]     ┆ 1   ┆ [null, 1, 2]    ┆ [null, null, 1] │
#> │ [2, null, 5]  ┆ 2   ┆ [null, null, 2] ┆ [null, null, 2] │
#> └───────────────┴─────┴─────────────────┴─────────────────┘