Skip to content

Commit e797a2d

Browse files
committed
BUG: freqstr may be parsed incorrectly
1 parent b7abef4 commit e797a2d

File tree

3 files changed

+215
-183
lines changed

3 files changed

+215
-183
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ Bug Fixes
963963
- Bug in ``Index.union`` returns an incorrect result with a named empty index (:issue:`13432`)
964964
- Bugs in ``Index.difference`` and ``DataFrame.join`` raise in Python3 when using mixed-integer indexes (:issue:`13432`, :issue:`12814`)
965965
- Bug in ``.to_excel()`` when DataFrame contains a MultiIndex which contains a label with a NaN value (:issue:`13511`)
966+
- Bug in invalid frequency offset string like "D1", "-2-3H" may not raise ``ValueError (:issue:`13930`)
966967

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

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('includes garbage characters')
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)