我正在对具有特定min_periods
的滚动窗口应用自定义函数。 pandas..rolling.apply
的输出符合我的期望;但是,xarray..rolling.reduce
的输出似乎没有考虑min_periods
。
MWE:
import numpy as np
import xarray
def custom(x, axis=0):
return np.mean(x, axis)
d = pd.DataFrame(np.random.rand(100,3))
r = d.rolling(10, min_periods=5).apply(custom)
print(r.iloc[0:10,:])
xd = d.to_xarray().to_array()
r = xd.rolling(index=10, min_periods=5).reduce(custom)
print(r[:,0:10])
r = xd.rolling(index=10, min_periods=1).reduce(custom)
print(r[:,0:10])
输出:
0 1 2
0 NaN NaN NaN
1 NaN NaN NaN
2 NaN NaN NaN
3 NaN NaN NaN
4 0.632168 0.523669 0.543643
5 0.558694 0.565781 0.481204
6 0.559343 0.541787 0.415490
7 0.613457 0.554888 0.398999
8 0.579552 0.496799 0.397681
9 0.562591 0.525096 0.416461
<xarray.DataArray (variable: 3, index: 10)>
array([[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.562591],
[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.525096],
[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.416461]])
Coordinates:
* index (index) int64 0 1 2 3 4 5 6 7 8 9
* variable (variable) int64 0 1 2
<xarray.DataArray (variable: 3, index: 10)>
array([[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.562591],
[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.525096],
[ nan, nan, nan, nan, nan, nan, nan,
nan, nan, 0.416461]])
Coordinates:
* index (index) int64 0 1 2 3 4 5 6 7 8 9
* variable (variable) int64 0 1 2