polars shift for [dataframe or lazyframe](/commands/categories/dataframe or lazyframe.md)
Shifts the values by a given period.
This command requires a plugin
The polars shift command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.
Signature
> polars shift {flags} (period)
Flags
- --fill, -f {any}: Expression used to fill the null values (lazy df)
Parameters
- period: shift period
Input/output types:
| input | output | 
|---|---|
| polars_dataframe | polars_dataframe | 
| polars_lazyframe | polars_lazyframe | 
| polars_expression | polars_expression | 
Examples
Shifts the values by a given period
> [1 2 2 3 3] | polars into-df | polars shift 2 | polars drop-nulls
╭───┬───╮
│ # │ 0 │
├───┼───┤
│ 0 │ 1 │
│ 1 │ 2 │
│ 2 │ 2 │
╰───┴───╯Shifts the values by a given period, fill absent values with 0
> [1 2 2 3 3] | polars into-lazy | polars shift 2 --fill 0 | polars collect
╭───┬───╮
│ # │ 0 │
├───┼───┤
│ 0 │ 0 │
│ 1 │ 0 │
│ 2 │ 1 │
│ 3 │ 2 │
│ 4 │ 2 │
╰───┴───╯Shift values of a column, fill absent values with 0
> [[a]; [1] [2] [2] [3] [3]]
                    | polars into-lazy
                    | polars with-column {b: (polars col a | polars shift 2 --fill 0)}
                    | polars collect
╭───┬───┬───╮
│ # │ a │ b │
├───┼───┼───┤
│ 0 │ 1 │ 0 │
│ 1 │ 2 │ 0 │
│ 2 │ 2 │ 1 │
│ 3 │ 3 │ 2 │
│ 4 │ 3 │ 2 │
╰───┴───┴───╯