Skip to content

Commit 7a8d224

Browse files
sinhrksjreback
authored andcommitted
BUG: freqstr may be parsed incorrectly
Author: sinhrks <[email protected]> Closes #13930 from sinhrks/freq_invalid and squashes the following commits: e797a2d [sinhrks] BUG: freqstr may be parsed incorrectly
1 parent 3760f16 commit 7a8d224

File tree

3 files changed

+216
-184
lines changed

3 files changed

+216
-184
lines changed

doc/source/whatsnew/v0.19.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ Bug Fixes
957957
- Bug in ``DatetimeIndex`` with nanosecond frequency does not include timestamp specified with ``end`` (:issue:`13672`)
958958

959959
- Bug in ``Index`` raises ``OutOfBoundsDatetime`` if ``datetime`` exceeds ``datetime64[ns]`` bounds, rather than coercing to ``object`` dtype (:issue:`13663`)
960-
- Bug in ``Index`` may ignores specified ``datetime64`` or ``timedelta64`` ``dtype`` (:issue:`13981`)
960+
- Bug in ``Index`` may ignore specified ``datetime64`` or ``timedelta64`` passed as ``dtype`` (:issue:`13981`)
961961
- Bug in ``RangeIndex`` can be created without no arguments rather than raises ``TypeError`` (:issue:`13793`)
962962
- Bug in ``.value_counts`` raises ``OutOfBoundsDatetime`` if data exceeds ``datetime64[ns]`` bounds (:issue:`13663`)
963963
- Bug in ``DatetimeIndex`` may raise ``OutOfBoundsDatetime`` if input ``np.datetime64`` has other unit than ``ns`` (:issue:`9114`)
@@ -977,6 +977,7 @@ Bug Fixes
977977
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
978978
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)
979979
- Bug in ``.to_excel()`` when DataFrame contains a MultiIndex which contains a label with a NaN value (:issue:`13511`)
980+
- Bug in invalid frequency offset string like "D1", "-2-3H" may not raise ``ValueError (:issue:`13930`)
980981

981982
- Bug in ``agg()`` function on groupby dataframe changes dtype of ``datetime64[ns]`` column to ``float64`` (:issue:`12821`)
982983

pandas/tseries/frequencies.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,14 @@ def to_offset(freq):
464464
delta = None
465465
stride_sign = None
466466
try:
467-
for stride, name, _ in opattern.findall(freq):
467+
splitted = re.split(opattern, freq)
468+
if splitted[-1] != '' and not splitted[-1].isspace():
469+
# the last element must be blank
470+
raise ValueError('last element must be blank')
471+
for sep, stride, name in zip(splitted[0::4], splitted[1::4],
472+
splitted[2::4]):
473+
if sep != '' and not sep.isspace():
474+
raise ValueError('separator must be spaces')
468475
offset = get_offset(name)
469476
if stride_sign is None:
470477
stride_sign = -1 if stride.startswith('-') else 1
@@ -486,7 +493,7 @@ def to_offset(freq):
486493

487494

488495
# hack to handle WOM-1MON
489-
opattern = re.compile(r'([\-]?\d*)\s*([A-Za-z]+([\-@][\dA-Za-z\-]+)?)')
496+
opattern = re.compile(r'([\-]?\d*)\s*([A-Za-z]+([\-][\dA-Za-z\-]+)?)')
490497

491498

492499
def _base_and_stride(freqstr):

0 commit comments

Comments
 (0)