diff --git a/doc/source/release.rst b/doc/source/release.rst index a2015a3b361ac..f1f685850284c 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -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 ------------- diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index cfe874484231b..a7e315af5c1cc 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -337,7 +337,7 @@ def get_base_alias(freqstr): """ return _base_and_stride(freqstr)[0] -_dont_uppercase = ['MS', 'ms'] +_dont_uppercase = set(('MS', 'ms')) def get_offset(name): @@ -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"] @@ -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: diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index 312a88bcbc5a9..84b62e7931156 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -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 @@ -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") + tm.assertRaisesRegexp(ValueError, "Unknown freqstr: MS", pd.Period, '2013-01', 'MS') + self.assertTrue(_period_code_map.get("MS") is None) def noWrap(item): return item