Create previous-value features
Feature engineering
Polars · Module
Shifts expression values by a chosen number of row positions. Positive offsets behave like SQL LAG, negative offsets like LEAD, and fill_value can replace boundary nulls.
Expr.shift returns an expression whose values are moved by a specified number of row positions. Positive offsets move values forward and create leading nulls, while negative offsets move values in the opposite direction; fill_value can replace the nulls introduced by the shift.
Expr.shift(*n: int | IntoExprColumn = 1*, ***, *fill_value: IntoExpr | None = None*) → Expr| Name | Type | Description |
|---|---|---|
n | int | IntoExprColumn | Number of indices to shift forward; negative values shift in the opposite direction. |
fill_value | IntoExpr | None | Scalar value used to fill nulls produced by the shift. |
An Expr containing the shifted values.
import polars as pl
df = pl.DataFrame({"a": [10, 20, 30, 40]})
result = df.with_columns(
previous=pl.col("a").shift(),
next=pl.col("a").shift(-1),
)
print(result)Creates lagged and lead values from column a; boundary positions remain null.
Feature engineering
Time-series analysis
Data cleaning
Window-style calculations
Expr.shift moves expression values by row position. The default offset is 1, which produces a lag-like result and nulls at the beginning; a negative offset produces a lead-like result.
Follow-up: How would you avoid null boundary values? Pass fill_value to Expr.shift, or apply fill_null when a separate replacement step is preferable.
The n argument controls the displacement and defaults to 1. Positive n shifts forward, while negative n shifts in the opposite direction.
Follow-up: What happens when n is -2? Values are taken from two positions ahead, and the final two positions become null unless filled.
fill_value supplies the scalar used for null values produced by shifting. It is optional and is applied to the resulting null positions.
Follow-up: When might fill_null be useful instead? Use it when null replacement should be expressed as a separate operation or use a more general expression.
polars.Expr.fill_nullpolars.Series.shiftpolars.DataFrame.shiftpolars.Expr.list.shift