Skip to content

Commit 75c1fa0

Browse files
frexvahijreback
authored andcommitted
ENH: allow using '+' sign in the argument to to_offset() (pandas-dev#18171)
1 parent 276b3c3 commit 75c1fa0

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

doc/source/whatsnew/v0.22.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Other Enhancements
2323
^^^^^^^^^^^^^^^^^^
2424

2525
- Better support for ``Dataframe.style.to_excel()`` output with the ``xlsxwriter`` engine. (:issue:`16149`)
26-
-
26+
- :func:`pd.tseries.frequencies.to_offset()` now accepts leading '+' signs e.g. '+1h'. (:issue:`18171`)
2727
-
2828

2929
.. _whatsnew_0220.api_breaking:

pandas/_libs/tslibs/frequencies.pyx

+1-1
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

+13
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,19 @@ 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+
181+
for bad_freq in ['+-1d', '-+1h', '+1', '-7', '+d', '-m']:
182+
with tm.assert_raises_regex(ValueError, 'Invalid frequency:'):
183+
frequencies.to_offset(bad_freq)
184+
172185
def test_to_offset_pd_timedelta(self):
173186
# Tests for #9064
174187
td = Timedelta(days=1, seconds=1)

0 commit comments

Comments
 (0)