使用 Pandas 1.0,我需要生成一个滚动最大值,其中包含前 3 个观察值的窗口,不包括当前观察值。在 R 中,这是通过
library(tidyverse)
test_df = data.frame(a = 1:5, b = c(40, 37, 60, 45, 40))
test_df <- test_df %>% mutate(
rolling_max=rollapply(b, width = list(-1:-3), max, na.rm = TRUE, partial = 0, align = "right")
)
> test_df
a b rolling_max
1 1 40 -Inf
2 2 37 40
3 3 60 40
4 4 45 60
5 5 40 60
在 Python 中,pandas.rolling.apply() 函数似乎没有排除当前观察的方法,因此这会产生意想不到的结果:
import pandas as pd
test_df = pd.DataFrame({'a': [1,2,3,4,5], 'b': [40,37,60,45,40]})
test_df['rolling_max'] = test_df['b'].rolling(3).apply(max)
test_df
a b rolling_max
0 1 40 NaN
1 2 37 NaN
2 3 60 60.0
3 4 45 60.0
4 5 40 60.0
这会输出预期的结果,但它看起来像是一个笨拙且不可扩展的解决方案:
test_df['rolling_max'] = np.fmax(
test_df['b'].shift(periods=1).to_numpy(),
test_df['b'].shift(periods=2).to_numpy(),
test_df['b'].shift(periods=3).to_numpy()
)
test_df
a b rolling_max
0 1 40 NaN
1 2 37 40.0
2 3 60 40.0
3 4 45 60.0
4 5 40 60.0
有人可以推荐更好的方法吗?