我想使用 pandas 在不规则时间序列上使用时间窗口计算移动平均值。理想情况下,应该使用 pandas.DataFrame.ewm 对窗口进行指数加权。 ,但参数(例如 span
)不接受基于时间的窗口。
如果我们尝试使用 pandas.DataFrame.rolling , 我们意识到我们不能将基于时间的窗口与 win_type 结合起来。
dft = pd.DataFrame({'B': [0, 1, 2, 3, 4]},
index = pd.Index([pd.Timestamp('20130101 09:00:00'),
pd.Timestamp('20130101 09:00:02'),
pd.Timestamp('20130101 09:00:03'),
pd.Timestamp('20130101 09:00:05'),
pd.Timestamp('20130101 09:00:06')],
name='foo'))
dft.rolling('2s', win_types='triang').sum()
>>> ValueError: Invalid window 2s
如何计算不规则时间序列的非均等加权基于时间的移动平均值?
与 '2s'
窗口关联的 dft.ewm(alpha=0.9, adjust=False).sum()
的预期输出将是 [0*1, 1*1, 2*1+1*0.9, 3*1, 4*1+3*0.9]
最佳答案
来自documentation似乎参数窗口必须是样本数而不是您想要的时间间隔。 也许您可以尝试重新采样您的时间序列以获得“常规”时间序列。 像这样:
dft = pd.DataFrame({'B': [0, 1, 2, 3, 4]},
index = pd.Index([pd.Timestamp('20130101 09:00:00'),
pd.Timestamp('20130101 09:00:02'),
pd.Timestamp('20130101 09:00:03'),
pd.Timestamp('20130101 09:00:05'),
pd.Timestamp('20130101 09:00:06')],
name='foo'))
dft = dft.resample(rule='Xs').mean()
dft.rolling(Y, win_type='triang').sum()
其中 X 是重采样时间增量,Y 是窗口 参数的整数。
关于python - Pandas .rolling 指定时间窗口和 win_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47377581/