Skip to content

Shift list values by n indices

Source code

Description

Shift list values by n indices

Usage

<Expr>$list$shift(n = 1)

Arguments

n Number of indices to shift forward. If a negative value is passed, values are shifted in the opposite direction instead.

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]  │
#> └─────────────┴─────┴──────────────────┴───────────────────┘