Skip to content

Commit b20de97

Browse files
committed
Allow using '+' sign in the argument to to_offset()
1 parent 5350330 commit b20de97

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

pandas/_libs/tslibs/frequencies.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ from util cimport is_integer_object
1515

1616
# hack to handle WOM-1MON
1717
opattern = re.compile(
18-
r'([\-]?\d*|[\-]?\d*\.\d*)\s*([A-Za-z]+([\-][\dA-Za-z\-]+)?)'
18+
r'([+\-]?\d*|[+\-]?\d*\.\d*)\s*([A-Za-z]+([\-][\dA-Za-z\-]+)?)'
1919
)
2020

2121
_INVALID_FREQ_ERROR = "Invalid frequency: {0}"

pandas/tests/tseries/test_frequencies.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,15 @@ def test_to_offset_leading_zero(self):
169169
result = frequencies.to_offset(freqstr)
170170
assert (result.n == -194)
171171

172+
def test_to_offset_leading_plus(self):
173+
freqstr = '+1d'
174+
result = frequencies.to_offset(freqstr)
175+
assert (result.n == 1)
176+
177+
freqstr = '+2h30min'
178+
result = frequencies.to_offset(freqstr)
179+
assert (result.n == 150)
180+
172181
def test_to_offset_pd_timedelta(self):
173182
# Tests for #9064
174183
td = Timedelta(days=1, seconds=1)

pandas/tseries/frequencies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ def to_offset(freq):
445445
delta = offset
446446
else:
447447
delta = delta + offset
448-
except Exception:
449-
raise ValueError(_INVALID_FREQ_ERROR.format(freq))
448+
except Exception as e:
449+
raise ValueError(_INVALID_FREQ_ERROR.format(freq)) from e
450450

451451
if delta is None:
452452
raise ValueError(_INVALID_FREQ_ERROR.format(freq))

0 commit comments

Comments
 (0)