在
pandas
中,我可以调用 data.expanding(min_periods=1).apply(lambda_func)
来调用扩展函数或类似 cumsum 的视图。
如何用极坐标做同样的事情?我只能找到
rolling_apply
或apply
。
从
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 │
│ --- │
│ i64 │
╞════════╡
│ 0 │
│ -3 │
│ -8 │
│ -15 │
│ -24 │
└────────┘