在 pandas
中,我可以调用 data.expanding(min_periods=1).apply(lambda_func)
来调用扩展函数或类似 cumsum 的视图。
如何用极性做同样的事情?我只能找到 rolling_apply
或 apply
。
回答1
从 polars>=0.13.36
您可以运行 cumulative_eval
在累积递增窗口上运行极坐标表达式。
请注意,您不应将其用于像 sum
这样的聚合,因为这将具有 O(n^2)
复杂性。
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 │
└────────┘