polars.LazyFrame.shift

LazyFrame.shift(periods: int) 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 Nones.

Parameters
periods

Number of places to shift (may be negative).

Examples

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