diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py index cfe874484231b..3892897e43bb0 100644 --- a/pandas/tseries/frequencies.py +++ b/pandas/tseries/frequencies.py @@ -358,7 +358,7 @@ def get_offset(name): else: if name in _rule_aliases: name = _rule_aliases[name] - + if name not in _offset_map: try: # generate and cache offset @@ -625,7 +625,7 @@ def _period_str_to_code(freqstr): alias = _period_alias_dict[freqstr] except KeyError: raise ValueError("Unknown freqstr: %s" % freqstr) - + return _period_code_map[alias] @@ -647,6 +647,10 @@ def infer_freq(index, warn=True): from pandas.tseries.index import DatetimeIndex if not isinstance(index, DatetimeIndex): + from pandas.tseries.period import PeriodIndex + if isinstance(index, PeriodIndex): + raise ValueError("PeriodIndex given. Check the `freq` attribute " + "instead of using infer_freq.") index = DatetimeIndex(index) inferer = _FrequencyInferer(index, warn=warn) @@ -850,7 +854,7 @@ def _get_wom_rule(self): weekdays = unique(self.index.weekday) if len(weekdays) > 1: return None - + week_of_months = unique((self.index.day - 1) // 7) if len(week_of_months) > 1: return None diff --git a/pandas/tseries/tests/test_frequencies.py b/pandas/tseries/tests/test_frequencies.py index ad9c93592a26c..8d95e22e4c6f2 100644 --- a/pandas/tseries/tests/test_frequencies.py +++ b/pandas/tseries/tests/test_frequencies.py @@ -13,6 +13,7 @@ from pandas.tseries.tools import to_datetime import pandas.tseries.frequencies as fmod import pandas.tseries.offsets as offsets +from pandas.tseries.period import PeriodIndex import pandas.lib as lib @@ -88,6 +89,10 @@ def test_anchored_shortcuts(): class TestFrequencyInference(tm.TestCase): + def test_raise_if_period_index(self): + index = PeriodIndex(start="1/1/1990", periods=20, freq="M") + self.assertRaises(ValueError, infer_freq, index) + def test_raise_if_too_few(self): index = _dti(['12/31/1998', '1/3/1999']) self.assertRaises(ValueError, infer_freq, index)