Python-熊猫-重新采样问题

时间:2018-07-07 21:59:28

标签: python pandas

我正在尝试将具有特定频率的Pandas.Series适应具有不同频率的Pandas.Series。因此,我使用了重采样功能,但无法识别例如“ M”是“ 3M”的子句并引发了错误

import pandas as pd

idx_1 = pd.period_range('2017-01-01', periods=6, freq='M')
data_1  = pd.Series(range(6), index=idx_1)
data_higher_freq = data_1.resample('3M', kind="Period").sum()

引发以下异常:

Traceback (most recent call last):  File "/home/mitch/Programs/Infrastructure_software/Sandbox/spyderTest.py", line 15, in <module>
data_higher_freq = data_1.resample('3M', kind="Period").sum() File "/home/mitch/anaconda3/lib/python3.6/site-packages/pandas/core/resample.py", line 758, in f return self._downsample(_method, min_count=min_count) File "/home/mitch/anaconda3/lib/python3.6/site-packages/pandas/core/resamplepy", line 1061, in _downsample 'sub or super periods'.format(ax.freq, self.freq))
pandas._libs.tslibs.period.IncompatibleFrequency: Frequency <MonthEnd> cannot be resampled to <3 * MonthEnds>, as they are not sub or super periods

这似乎是由于pd.tseries.frequencies.is_subperiod函数引起的:

import pandas as pd

pd.tseries.frequencies.is_subperiod('M', '3M')
pd.tseries.frequencies.is_subperiod('M', 'Q')

实际上,它对第一个命令返回False,对第二个命令返回True。

我非常感谢有关任何解决方案的任何提示。

谢谢。

1 个答案:

答案 0 :(得分:0)

在重新采样之前,尝试从PeriodIndex更改为DateTimeIndex

import pandas as pd

idx_1 = pd.period_range('2017-01-01', periods=6, freq='M')
data_1  = pd.Series(range(6), index=idx_1)
data_1.index = data_1.index.astype('datetime64[ns]')
data_higher_freq = data_1.resample('3M', kind='period').sum()

输出:

data_higher_freq
Out[582]: 
2017-01     3
2017-04    12
Freq: 3M, dtype: int64