是否有 ewm 和滚动方法的参数不填写 NA?
>>> A = pd.Series([1,2,3,4,np.nan,5,6])
>>> A
0 1.0
1 2.0
2 3.0
3 4.0
4 NaN
5 5.0
6 6.0
dtype: float64
>>> eA = A.ewm(alpha = 0.5, ignore_na=True).mean()
>>> eA
0 1.000000
1 1.666667
2 2.428571
3 3.266667
4 3.266667 # I want this to be NA, don't fill in
5 4.161290
6 5.095238
dtype: float64
当然这很容易解决
eA[A.isnull()] = np.nan
但这会花费一些不必要的运行时间,并且当您有多个滚动函数时,需要为每个滚动函数考虑一个变量名称,这很麻烦。
最佳答案
不幸的是,ewm
目前不支持此功能。 ,您必须使用建议的方法用 NaN
覆盖或过滤 NaN
行,以便 ewm
生成 Series
保留原始索引,然后您可以使用 combine_first
或reindex
重新插入 NaN
行:
In [32]:
A = pd.Series([1,2,3,4,np.nan,5,6])
eA = A[A.notnull()].ewm(alpha = 0.5, ignore_na=True).mean()
eA.combine_first(A)
Out[32]:
0 1.000000
1 1.666667
2 2.428571
3 3.266667
4 NaN
5 4.161290
6 5.095238
dtype: float64
In [33]:
eA.reindex(A.index)
Out[33]:
0 1.000000
1 1.666667
2 2.428571
3 3.266667
4 NaN
5 4.161290
6 5.095238
dtype: float64
关于python - pandas ewm 和滚动方法不填写 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39975812/