Skip to content

Shift and fill

Source code

Description

Shift and fill the values by a given period.

Usage

<GroupBy>$shift_and_fill(fill_value, periods = 1)

Arguments

fill_value fill None values with the result of this expression.
periods integer Number of periods to shift (may be negative).

Value

GroupBy

Examples

library(polars)

pl$DataFrame(mtcars)$group_by("cyl")$shift_and_fill(99, 1)
#> 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] │
#> ╞═════╪════════════╪════════════╪════════════╪═══╪════════════╪════════════╪═══════════╪═══════════╡
#> │ 8.0 ┆ [99.0,     ┆ [99.0,     ┆ [99.0,     ┆ … ┆ [99.0,     ┆ [99.0,     ┆ [99.0,    ┆ [99.0,    │
#> │     ┆ 18.7, …    ┆ 360.0, …   ┆ 175.0, …   ┆   ┆ 0.0, …     ┆ 0.0, …     ┆ 3.0, …    ┆ 2.0, …    │
#> │     ┆ 15.8]      ┆ 351.0]     ┆ 264.0]     ┆   ┆ 0.0]       ┆ 1.0]       ┆ 5.0]      ┆ 4.0]      │
#> │ 4.0 ┆ [99.0,     ┆ [99.0,     ┆ [99.0,     ┆ … ┆ [99.0,     ┆ [99.0,     ┆ [99.0,    ┆ [99.0,    │
#> │     ┆ 22.8, …    ┆ 108.0, …   ┆ 93.0, …    ┆   ┆ 1.0, …     ┆ 1.0, …     ┆ 4.0, …    ┆ 1.0, …    │
#> │     ┆ 30.4]      ┆ 95.1]      ┆ 113.0]     ┆   ┆ 1.0]       ┆ 1.0]       ┆ 5.0]      ┆ 2.0]      │
#> │ 6.0 ┆ [99.0,     ┆ [99.0,     ┆ [99.0,     ┆ … ┆ [99.0,     ┆ [99.0,     ┆ [99.0,    ┆ [99.0,    │
#> │     ┆ 21.0, …    ┆ 160.0, …   ┆ 110.0, …   ┆   ┆ 0.0, …     ┆ 1.0, …     ┆ 4.0, …    ┆ 4.0, …    │
#> │     ┆ 17.8]      ┆ 167.6]     ┆ 123.0]     ┆   ┆ 1.0]       ┆ 0.0]       ┆ 4.0]      ┆ 4.0]      │
#> └─────┴────────────┴────────────┴────────────┴───┴────────────┴────────────┴───────────┴───────────┘