我有一个带有每日 DatetimeIndex 的 Pandas DataFrame。我正在尝试应用 Resample 方法将值汇总为每月系列,如下所示:
>>> aggVols.resample('M',axis=1).sum()
但是当我尝试这个时,我得到了错误
TypeError: Only valid with DatetimeIndex or PeriodIndex
我注意到没有设置对象索引的频率(无)。
>>>aggVols.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2016-01-04, ..., 2016-07-01]
Length: 130, Freq: None, Timezone: None
所以我首先将频率设置为每天(工作日)并重置索引,以便我可以应用重新采样:
>>> aggVols = aggVols.reindex(aggVols.asfreq('B').index)
>>> aggVols.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2016-01-04, ..., 2016-07-01]
Length: 130, Freq: B, Timezone: None
但是我仍然遇到与 resample 函数相同的错误:
TypeError: Only valid with DatetimeIndex or PeriodIndex
索引有什么问题?为什么无效?如果我将频率设置为 D,我会得到同样的错误。
谢谢!