polars.LazyFrame.shift_and_fill#

LazyFrame.shift_and_fill(periods: int, fill_value: polars.internals.expr.expr.Expr | int | str | float) LDF[source]#

Shift the values by a given period and fill the resulting null values.

Parameters:
periods

Number of places to shift (may be negative).

fill_value

fill None values with the result of this expression.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 3, 5],
...         "b": [2, 4, 6],
...     }
... ).lazy()
>>> df.shift_and_fill(periods=1, fill_value=0).collect()
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 0   ┆ 0   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 1   ┆ 2   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 3   ┆ 4   │
└─────┴─────┘
>>> df.shift_and_fill(periods=-1, fill_value=0).collect()
shape: (3, 2)
┌─────┬─────┐
│ a   ┆ b   │
│ --- ┆ --- │
│ i64 ┆ i64 │
╞═════╪═════╡
│ 3   ┆ 4   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 5   ┆ 6   │
├╌╌╌╌╌┼╌╌╌╌╌┤
│ 0   ┆ 0   │
└─────┴─────┘