polars.DataFrame.shift_and_fill#

DataFrame.shift_and_fill(
fill_value: int | str | float,
*,
periods: int = 1,
) DataFrame[source]#

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

Parameters:
fill_value

fill None values with this value.

periods

Number of places to shift (may be negative).

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> df.shift_and_fill(periods=1, fill_value=0)
shape: (3, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ 0   ┆ 0   ┆ 0   │
│ 1   ┆ 6   ┆ a   │
│ 2   ┆ 7   ┆ b   │
└─────┴─────┴─────┘