Shift and fill values
Description
Shift the values by a given period and fill the resulting null values.
Usage
<Expr>$shift_and_fill(periods, fill_value)
Arguments
periods
|
Number of periods to shift, may be negative. |
fill_value
|
Fill null values with the result of this expression. |
Value
Expr
Examples
library(polars)
pl$DataFrame(a = c(1, 2, 4, 5, 8))$
with_columns(
pl$col("a")$shift_and_fill(-2, fill_value = 42)$alias("shift-2"),
pl$col("a")$shift_and_fill(2, fill_value = pl$col("a") / 2)$alias("shift+2")
)
#> shape: (5, 3)
#> ┌─────┬─────────┬─────────┐
#> │ a ┆ shift-2 ┆ shift+2 │
#> │ --- ┆ --- ┆ --- │
#> │ f64 ┆ f64 ┆ f64 │
#> ╞═════╪═════════╪═════════╡
#> │ 1.0 ┆ 4.0 ┆ 0.5 │
#> │ 2.0 ┆ 5.0 ┆ 0.5 │
#> │ 4.0 ┆ 8.0 ┆ 1.0 │
#> │ 5.0 ┆ 42.0 ┆ 2.0 │
#> │ 8.0 ┆ 42.0 ┆ 4.0 │
#> └─────┴─────────┴─────────┘