Repository: pandas-dev/pandas

Issue:
infer_freq borks on PeriodIndex
Should PeriodIndex pass through infer_freq and return freqstr or should this be the user's problem? Either is a simple fix. The latter should just have a better error message. Right now

```
from pandas.tseries.api import infer_freq
from pandas import PeriodIndex

infer_freq(PeriodIndex(start="1/1/1990", periods=20, freq="M"))
```

infer_freq borks on PeriodIndex
Should PeriodIndex pass through infer_freq and return freqstr or should this be the user's problem? Either is a simple fix. The latter should just have a better error message. Right now

```
from pandas.tseries.api import infer_freq
from pandas import PeriodIndex

infer_freq(PeriodIndex(start="1/1/1990", periods=20, freq="M"))
```



Discussion:
inferring is not implemented for PeriodIndex AFAICT

```
In [11]: p = PeriodIndex(start="1/1/1990", periods=20, freq="M")

In [12]: p.freqstr
Out[12]: 'M'

In [13]: p.freq
Out[13]: 'M'
```

Yes. Should it be? I.e., trivially return freqstr since you don't need to do any inferring or have a better error message than the one about converting Int64Index to Datetime. Use case is for time series models in statsmodels. Should we handle it or should infer_freq handle it? (I've already committed a fix, so I don't really care either way.)

I think the use would be if someone puts together multiple Periods and tries to create a PeriodIndex

But I don't think very comment (unlike doing this with Timestamps, which is pretty common)

and unless they are ALL the same freq it would bork anyhow.

So maybe just a better error message.

inferring is not implemented for PeriodIndex AFAICT

```
In [11]: p = PeriodIndex(start="1/1/1990", periods=20, freq="M")

In [12]: p.freqstr
Out[12]: 'M'

In [13]: p.freq
Out[13]: 'M'
```

Yes. Should it be? I.e., trivially return freqstr since you don't need to do any inferring or have a better error message than the one about converting Int64Index to Datetime. Use case is for time series models in statsmodels. Should we handle it or should infer_freq handle it? (I've already committed a fix, so I don't really care either way.)

I think the use would be if someone puts together multiple Periods and tries to create a PeriodIndex

But I don't think very comment (unlike doing this with Timestamps, which is pretty common)

and unless they are ALL the same freq it would bork anyhow.

So maybe just a better error message.


Resolving patch:
diff --git a/pandas/tseries/frequencies.py b/pandas/tseries/frequencies.py
--- 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


Test patch:
diff --git a/pandas/tseries/tests/test_frequencies.py b/pandas/tseries/tests/test_frequencies.py
--- 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)