Skip to content

More informative exception when trying to use MS as period frequency #5340

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ Bug Fixes
- Fixed issue with ``drop`` and a non-unique index on Series (:issue:`5248`)
- Fixed seg fault in C parser caused by passing more names than columns in
the file. (:issue:`5156`)
- More informative exception when trying to use ``MS`` as period frequency (:issue:`5332`)

pandas 0.12.0
-------------
Expand Down
9 changes: 6 additions & 3 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def _period_alias_dictionary():
H_aliases = ["H", "HR", "HOUR", "HRLY", "HOURLY"]
T_aliases = ["T", "MIN", "MINUTE", "MINUTELY"]
S_aliases = ["S", "SEC", "SECOND", "SECONDLY"]
L_aliases = ["L", "MS", "MILLISECOND", "MILLISECONDLY"]
L_aliases = ["L", "ms", "MILLISECOND", "MILLISECONDLY"]
U_aliases = ["U", "US", "MICROSECOND", "MICROSECONDLY"]
N_aliases = ["N", "NS", "NANOSECOND", "NANOSECONDLY"]

Expand Down Expand Up @@ -615,10 +615,13 @@ def _period_group(freqstr):
def _period_str_to_code(freqstr):
# hack
freqstr = _rule_aliases.get(freqstr, freqstr)
freqstr = _rule_aliases.get(freqstr.lower(), freqstr)

if freqstr not in _dont_uppercase:
freqstr = _rule_aliases.get(freqstr.lower(), freqstr)

try:
freqstr = freqstr.upper()
if freqstr not in _dont_uppercase:
freqstr = freqstr.upper()
return _period_code_map[freqstr]
except KeyError:
try:
Expand Down
11 changes: 9 additions & 2 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from numpy.ma.testutils import assert_equal

from pandas import Timestamp
from pandas.tseries.frequencies import MONTHS, DAYS
from pandas.tseries.frequencies import MONTHS, DAYS, _period_code_map
from pandas.tseries.period import Period, PeriodIndex, period_range
from pandas.tseries.index import DatetimeIndex, date_range, Index
from pandas.tseries.tools import to_datetime
Expand Down Expand Up @@ -488,7 +488,14 @@ def test_constructor_infer_freq(self):

p = Period('2007-01-01 07:10:15.123400')
self.assert_(p.freq == 'U')


def test_asfreq_MS(self):
initial = Period("2013")

self.assertEqual(initial.asfreq(freq="M", how="S"), Period('2013-01', 'M'))
self.assertRaises(ValueError, initial.asfreq, freq="MS", how="S")
Copy link
Contributor

Choose a reason for hiding this comment

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

please change one of these to have (a portion of) the text of the error message. Much easier to maintain that way (and adds really minimal performance hit).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

self.assertRaises(ValueError, pd.Period, '2013-01', 'MS')
self.assertTrue(_period_code_map.get("MS") is None)

def noWrap(item):
return item
Expand Down