Skip to content

Shift the values by a given period

Source code

Description

Shift the values by a given period

Usage

<GroupBy>$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

GroupBy

Examples

library("polars")

as_polars_df(mtcars)$group_by("cyl")$shift(2)
#> shape: (3, 11)
#> ┌─────┬────────────┬────────────┬────────────┬───┬────────────┬────────────┬───────────┬───────────┐
#> │ cyl ┆ mpg        ┆ disp       ┆ hp         ┆ … ┆ vs         ┆ am         ┆ gear      ┆ carb      │
#> │ --- ┆ ---        ┆ ---        ┆ ---        ┆   ┆ ---        ┆ ---        ┆ ---       ┆ ---       │
#> │ f64 ┆ list[f64]  ┆ list[f64]  ┆ list[f64]  ┆   ┆ list[f64]  ┆ list[f64]  ┆ list[f64] ┆ list[f64] │
#> ╞═════╪════════════╪════════════╪════════════╪═══╪════════════╪════════════╪═══════════╪═══════════╡
#> │ 6.0 ┆ [null,     ┆ [null,     ┆ [null,     ┆ … ┆ [null,     ┆ [null,     ┆ [null,    ┆ [null,    │
#> │     ┆ null, …    ┆ null, …    ┆ null, …    ┆   ┆ null, …    ┆ null, …    ┆ null, …   ┆ null, …   │
#> │     ┆ 19.2]      ┆ 167.6]     ┆ 123.0]     ┆   ┆ 1.0]       ┆ 0.0]       ┆ 4.0]      ┆ 4.0]      │
#> │ 8.0 ┆ [null,     ┆ [null,     ┆ [null,     ┆ … ┆ [null,     ┆ [null,     ┆ [null,    ┆ [null,    │
#> │     ┆ null, …    ┆ null, …    ┆ null, …    ┆   ┆ null, …    ┆ null, …    ┆ null, …   ┆ null, …   │
#> │     ┆ 19.2]      ┆ 400.0]     ┆ 175.0]     ┆   ┆ 0.0]       ┆ 0.0]       ┆ 3.0]      ┆ 2.0]      │
#> │ 4.0 ┆ [null,     ┆ [null,     ┆ [null,     ┆ … ┆ [null,     ┆ [null,     ┆ [null,    ┆ [null,    │
#> │     ┆ null, …    ┆ null, …    ┆ null, …    ┆   ┆ null, …    ┆ null, …    ┆ null, …   ┆ null, …   │
#> │     ┆ 26.0]      ┆ 120.3]     ┆ 91.0]      ┆   ┆ 0.0]       ┆ 1.0]       ┆ 5.0]      ┆ 2.0]      │
#> └─────┴────────────┴────────────┴────────────┴───┴────────────┴────────────┴───────────┴───────────┘