Skip to content

Shift list values by n indices

Source code

Description

Shift list values by n indices

Usage

<Expr>$list$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(
  s = list(1:4, c(10L, 2L, 1L)),
  idx = 1:2
)
df$with_columns(
  shift_by_expr = pl$col("s")$list$shift(pl$col("idx")),
  shift_by_lit = pl$col("s")$list$shift(2)
)
#> shape: (2, 4)
#> ┌─────────────┬─────┬──────────────────┬───────────────────┐
#> │ s           ┆ idx ┆ shift_by_expr    ┆ shift_by_lit      │
#> │ ---         ┆ --- ┆ ---              ┆ ---               │
#> │ list[i32]   ┆ i32 ┆ list[i32]        ┆ list[i32]         │
#> ╞═════════════╪═════╪══════════════════╪═══════════════════╡
#> │ [1, 2, … 4] ┆ 1   ┆ [null, 1, … 3]   ┆ [null, null, … 2] │
#> │ [10, 2, 1]  ┆ 2   ┆ [null, null, 10] ┆ [null, null, 10]  │
#> └─────────────┴─────┴──────────────────┴───────────────────┘