polars.Expr.diff#

Expr.diff(n: int = 1, null_behavior: NullBehavior = 'ignore') Expr[source]#

Calculate the n-th discrete difference.

Parameters:
n

Number of slots to shift.

null_behavior{‘ignore’, ‘drop’}

How to handle null values.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [20, 10, 30],
...     }
... )
>>> df.select(pl.col("a").diff())
shape: (3, 1)
┌──────┐
│ a    │
│ ---  │
│ i64  │
╞══════╡
│ null │
├╌╌╌╌╌╌┤
│ -10  │
├╌╌╌╌╌╌┤
│ 20   │
└──────┘