我有一个数据框,我用旧式滚动语法估计了各种类型的 10 年滚动平均值:
`pandas.rolling_mean(df['x'], 10)`,
`pandas.rolling_median(df['x'], 10)`
和
`pandas.rolling_apply(df['x'],10, hodgesLehmanMean)`,
其中 hodgesLehman mean 是我写的一个函数(见下文)。
def hodgesLehmanMean(x):
#Computes the Hodges-Lehman mean = median { [x_i + x+j]/2 }.
#Robust to 29% outliers, with high (95% efficiency) in the gaussian case
N = len(x)
return 0.5 * numpy.median(x[i] + x[j] for i in range(N) for j in range(i+1,N))
`
现在旧的滚动功能已被弃用,我正在尝试以新样式 series.rolling() 样式重写我的代码,即:
`df['x'].rolling(window=10).mean()`,
`df['x'].rolling(window=10).median()`
and
`df['x'].rolling(window=10).hodgesLehmanMean()`.
前两个(平均值和中值)就像一个魅力。第三个(hodgesLehmanMean)不起作用 - 它引发了AttributeError: 'Rolling' object has no attribute 'hodgesLehmanMean
如何让我的函数使用新的 series.rolling 语法?