In pandas
I could call data.expanding(min_periods=1).apply(lambda_func)
to call a func on expanding or a cumsum-like view.
How to do the same thing with polars? I could only find rolling_apply
or apply
.
In pandas
I could call data.expanding(min_periods=1).apply(lambda_func)
to call a func on expanding or a cumsum-like view.
How to do the same thing with polars? I could only find rolling_apply
or apply
.
From polars>=0.13.36
you can run cumulative_eval
to run polars expressions on a cumulative increasing window.
Note that you should not use this for an aggregation like sum
as this will have O(n^2)
complexity.
df = pl.DataFrame({
"values": [1, 2, 3, 4, 5]
})
df.select([
pl.col("values").cumulative_eval(pl.element().first() - pl.element().last() ** 2)
])
shape: (5, 1)
┌────────┐
│ values │
│ --- │
│ f64 │
╞════════╡
│ null │
├╌╌╌╌╌╌╌╌┤
│ -3.0 │
├╌╌╌╌╌╌╌╌┤
│ -8.0 │
├╌╌╌╌╌╌╌╌┤
│ -15.0 │
├╌╌╌╌╌╌╌╌┤
│ -24.0 │
└────────┘