polars.LazyFrame.shift_and_fill

LazyFrame.shift_and_fill(periods: int, fill_value: Union[polars.internals.expr.Expr, int, str, float]) polars.internals.lazy_frame.LDF

Shift the values by a given period and fill the parts that will be empty due to this operation with the result of the fill_value expression.

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   │
└─────┴─────┘