Skip to content

ENH: Improve error message for PeriodIndex to infer_freq. Closes #5841. #5847

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 1 commit into from
Jan 4, 2014
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
10 changes: 7 additions & 3 deletions pandas/tseries/frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]


Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pandas/tseries/tests/test_frequencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down