polars.Expr.shift_and_fill#

Expr.shift_and_fill(periods: int, fill_value: int | float | bool | str | Expr | list[Any]) Self[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({"foo": [1, 2, 3, 4]})
>>> df.select(pl.col("foo").shift_and_fill(1, "a"))
shape: (4, 1)
┌─────┐
│ foo │
│ --- │
│ str │
╞═════╡
│ a   │
│ 1   │
│ 2   │
│ 3   │
└─────┘