Skip to content

ENH: allow using '+' sign in the argument to to_offset() #18171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.22.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Other Enhancements
^^^^^^^^^^^^^^^^^^

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be pandas (and not pd)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other entries in the file all say 'pd' and not 'pandas'. In previous whatsnew.txt files there seems to be a mixture but still a preponderance of 'pd'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, then maybe this works. ok.

-

.. _whatsnew_0220.api_breaking:
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/tslibs/frequencies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ from util cimport is_integer_object

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

_INVALID_FREQ_ERROR = "Invalid frequency: {0}"
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/tseries/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ def test_to_offset_leading_zero(self):
result = frequencies.to_offset(freqstr)
assert (result.n == -194)

def test_to_offset_leading_plus(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a couple invalid freqstrs, like '+-1d', '-+1d', '+1', '+d' and ensure that they all raise.

At least I think they should all raise? d is apparently valid, but -d isn't.

freqstr = '+1d'
result = frequencies.to_offset(freqstr)
assert (result.n == 1)

freqstr = '+2h30min'
result = frequencies.to_offset(freqstr)
assert (result.n == 150)

for bad_freq in ['+-1d', '-+1h', '+1', '-7', '+d', '-m']:
with tm.assert_raises_regex(ValueError, 'Invalid frequency:'):
frequencies.to_offset(bad_freq)

def test_to_offset_pd_timedelta(self):
# Tests for #9064
td = Timedelta(days=1, seconds=1)
Expand Down