ENH: make period_range parse strings like '2012-Q1', not just '2012Q1' · Issue #9688 · pandas-dev/pandas (original) (raw)
This format is used, e.g., in datasets from the ECB.
Code example:
In [25]: PD.period_range(start = '2012Q1', periods = 4, freq = 'Q')
Out[25]:
<class 'pandas.tseries.period.PeriodIndex'>
[2012Q1, ..., 2012Q4]
Length: 4, Freq: Q-DEC
In [26]: PD.period_range(start = '2012-Q1', periods = 4, freq = 'Q')
ValueError Traceback (most recent call last)
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\tools.py in
parse_time_string(arg, freq, dayfirst, yearfirst)
463 parsed, reso = dateutil_parse(arg, default, dayfirst=dayfirst,
--> 464 yearfirst=yearfirst)
465 except Exception as e:
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\tools.py in
dateutil_parse(timestr, default, ignoretz, tzinfos, **kwargs)
489 if res is None:
--> 490 raise ValueError("unknown string format")
491
ValueError: unknown string format
During handling of the above exception, another exception occurred:
DateParseError Traceback (most recent call last)
in ()
----> 1 PD.period_range(start = '2012-Q1', periods = 4, freq = 'Q')
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\period.py i
n period_range(start, end, periods, freq, name)
1433 """
1434 return PeriodIndex(start=start, end=end, periods=periods,
-> 1435 freq=freq, name=name)
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\period.py i
n new(cls, data, ordinal, freq, start, end, periods, copy, name, tz, **kwarg
s)
637 else:
638 data, freq = cls._generate_range(start, end, periods,
--> 639 freq, kwargs)
640 else:
641 ordinal, freq = cls._from_arraylike(data, freq, tz)
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\period.py i
n _generate_range(cls, start, end, periods, freq, fields)
651 raise ValueError('Can either instantiate from fields '
652 'or endpoints, but not both')
--> 653 subarr, freq = _get_ordinal_range(start, end, periods, freq)
654 elif field_count > 0:
655 subarr, freq = _range_from_fields(freq=freq, **fields)
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\period.py i
n _get_ordinal_range(start, end, periods, freq)
1281
1282 if start is not None:
-> 1283 start = Period(start, freq)
1284 if end is not None:
1285 end = Period(end, freq)
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\period.py i
n init(self, value, freq, ordinal, year, month, quarter, day, hour, minute,
second)
124 value = value.upper()
125
--> 126 dt, _, reso = parse_time_string(value, freq)
127 if freq is None:
128 try:
C:\Users\stefan\Anaconda\envs\py343\lib\site-packages\pandas\tseries\tools.py in
parse_time_string(arg, freq, dayfirst, yearfirst)
465 except Exception as e:
466 # TODO: allow raise of errors within instead
--> 467 raise DateParseError(e)
468
469 if parsed is None:
DateParseError: unknown string format