Skip to content

Shift list values by the given number of indices

Description

Shift list values by the given number of 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

A polars expression

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