熊猫:带有年份的PeriodIndex开始锚定偏移量

时间:2018-10-30 19:28:02

标签: python pandas

我在创建带有参考年开始月份的锚定偏移量的熊猫PeriodIndex时遇到问题

使用年末月份创建一个PeriodIndex可以正常工作:

>>> per_ind = pd.PeriodIndex(start='2001-01-01', periods=1, freq='A-AUG')
>>> per_ind.start_time
DatetimeIndex(['2000-09-01'], dtype='datetime64[ns]', freq=None)
>>> per_ind.end_time
DatetimeIndex(['2001-08-31'], dtype='datetime64[ns]', freq=None)
>>> per_ind.year
Int64Index([2001], dtype='int64')

所以我们有一个一年期,到2001年8月底为止。这一年等于2001年

我想做的是创建一个时期,该时期开始于2001年8月,结束于一年之后,并且年份等于2001年(如果有人感兴趣,这是由于美国农业部报告的农作物年限; 2001年的农作物)例如,美国玉米的年份从2001-09-01:2002-08-31开始)

我以为这就像将freq参数更改为'AS_AUG'一样简单,但是:

>>>per_ind = pd.PeriodIndex(start='2001-01-01', periods=1, freq='AS-AUG')
Traceback (most recent call last):
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/tseries/frequencies.py", line 744, in _period_str_to_code
    return _period_code_map[freqstr]
KeyError: 'AS-AUG'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-28-fc801b3bbb96>", line 1, in <module>
    per_ind = pd.PeriodIndex(start='2001-01-01', periods=1, freq='AS-AUG')
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/core/indexes/period.py", line 236, in __new__
    freq, kwargs)
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/core/indexes/period.py", line 288, in _generate_range
    subarr, freq = _get_ordinal_range(start, end, periods, freq)
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/core/indexes/period.py", line 1065, in _get_ordinal_range
    _, mult = _gfc(freq)
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/tseries/frequencies.py", line 335, in get_freq_code
    code = _period_str_to_code(freqstr[0])
  File "/space/asd/conda/envs/rd/lib/python3.6/site-packages/pandas/tseries/frequencies.py", line 746, in _period_str_to_code
    raise ValueError(_INVALID_FREQ_ERROR.format(freqstr))
ValueError: Invalid frequency: AS-AUG

有人可以在这里提供任何指导吗?我可以在PeriodIndex上添加一个名为crop_year的属性,但这是很棘手的,应该有一种更整洁的方法来解决这个问题

编辑:

此问题出现在熊猫Period对象中-无法通过任何Period-start频率传递这些对象。有充分的理由吗?

0 个答案:

没有答案