Change analysis
Create period-over-period changes in a numeric column.
Polars · Module
Calculates discrete differences between expression values and values shifted by a specified number of positions. It can preserve or drop null results.
Expr.diff compares each value with the value a configurable number of positions earlier. It returns null for positions without a prior comparison; setting null_behavior to drop removes those null results instead of preserving them.
Expr.diff(*n: int | IntoExpr = 1*, *null_behavior: NullBehavior = 'ignore'*) → Expr| Name | Type | Description |
|---|---|---|
n | int | IntoExpr | Number of slots to shift. |
null_behavior | NullBehavior | How to handle null values; supported values are ignore and drop. |
Expr
ignore.with_columns to add a derived difference column.ignore and drop; choose explicitly when null handling affects result length.n value controls the comparison distance, so larger values produce more leading unavailable results.import polars as pl
df = pl.DataFrame({"value": [20, 10, 30, 25, 35]})
result = df.select(
pl.col("value").diff(n=2, null_behavior="drop").alias("change")
)
print(result)Computes two-step differences and removes the leading null results.
Create period-over-period changes in a numeric column.
Compare values with observations several rows earlier.
Build derived columns in a DataFrame transformation.
Expr.diff compute?Expr.diff calculates the discrete difference between shifted expression values. With the default n=1, each result compares the current value with the preceding value, producing a leading null where no predecessor exists.
Follow-up: How can the output omit unavailable leading results? Use null_behavior="drop", which returns only the computed differences.
n parameter?n specifies how many positions to shift when calculating the difference. For example, n=2 compares each value with the value two positions earlier.
Follow-up: What happens near the beginning with n=2? Positions lacking a value two slots earlier produce null unless dropped.
null_behavior affect results?null_behavior accepts ignore or drop. The choice determines how null values are handled and whether null results remain in the output.
Follow-up: Which option preserves the original result shape? ignore preserves the null results, while drop removes them.
polars.Series.diffpolars.Expr.list.diffpolars.Expr.shift