我有一个名为 dF 的数据框:
Twa_mean
Date_Time
2007-01-01 07:00:00 6.5
2007-01-01 07:10:00 6.4
2007-01-01 07:20:00 6.4
2007-01-01 07:30:00 NaN
2007-01-01 07:40:00 NaN
2007-01-01 07:50:00 NaN
我试图获得滚动平均值(window=2),但没有考虑 NaN,所以,我使用 scipy.stats 的 nanmean 函数:
import scipy.stats as st
f=lambda x: st.nanmean(x)
d=pd.rolling_apply(dF,2,f)
但是,我得到了这个:
Twa_mean
Date_Time
2007-01-01 07:00:00 NaN
2007-01-01 07:10:00 6.45
2007-01-01 07:20:00 6.40
2007-01-01 07:30:00 NaN
2007-01-01 07:40:00 NaN
2007-01-01 07:50:00 NaN
就像 st.nanmean 没有忽略 NaN,发生了什么?
我希望这样:
Twa_mean
Date_Time
2007-01-01 07:00:00 NaN
2007-01-01 07:10:00 6.45
2007-01-01 07:20:00 6.40
2007-01-01 07:30:00 6.40
2007-01-01 07:40:00 NaN
2007-01-01 07:50:00 NaN