我正在try 为返回Polars表达式的功能管道编写一个方法.该方法应将列名作为字符串和整数天数.我想使用等于天数的窗口对该列执行滚动计数.
似乎没有rolling_count
的表达式,所以我try 使用rolling_sum
,但没有用.
def temporal_rolling_count(col: str, days: int) -> pl.Expr:
return (
pl.lit(1)
.rolling_sum(window_size=f"{days}d", by="date_time")
.over(col)
.fill_null(0)
)
我也try 了这种方法,它更接近,但仍然不是在所有情况下都有效
def temporal_rolling_count(col: str, days: int) -> pl.Expr:
return (
pl.col(col)
.cumcount()
.over(col, (pl.col("date_time") - pl.col("date_time").min()).dt.days() % days == 0)
.fill_null(0)
)
有什么办法可以通过返回一个表达式来实现这一点吗?或者我必须直接对DataFrame执行操作,比如使用groupby_rolling
?