Repository: pandas-dev/pandas

Issue:
Iterating over groupby fails with period-indexed dataframe
I have found this while working with period-indexed data frames on Windows XP 32 bits:

```
import numpy as np
import pandas as pd
index = pd.period_range(start='1999-01', periods=5, freq='M')
s1 = pd.Series(np.random.rand(len(index)), index=index)
s2 = pd.Series(np.random.rand(len(index)), index=index)
series = [('s1', s1), ('s2',s2)]
df = pd.DataFrame.from_items(series)
grouped = df.groupby(df.index.month)
list(grouped)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\site-packages\pandas\core\groupby.py", line 595, in get_iterator
    for key, (i, group) in izip(keys, splitter):
  File "D:\Python27\lib\site-packages\pandas\core\groupby.py", line 2214, in __iter__
    sdata = self._get_sorted_data()
  File "D:\Python27\lib\site-packages\pandas\core\groupby.py", line 2231, in _get_sorted_data
    return self.data.take(self.sort_idx, axis=self.axis)
  File "D:\Python27\lib\site-packages\pandas\core\frame.py", line 2891, in take
    new_index = self.index.take(indices)
  File "D:\Python27\lib\site-packages\pandas\tseries\period.py", line 1110, in take
    taken = self.values.take(indices, axis=axis)
TypeError: Cannot cast array data from dtype('int64') to dtype('int32') according to the rule 'safe'
```

This happens with the last stable version 0.11.0. However iterating over grouped.s1 or grouped.s2 just works.



Discussion:
See [the docs for `numpy.ndarray.astype`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html) for why this is happening. Let `d1` and `d2` be two dtypes. `array([1,2], d1).astype(d2, cast='safe')` will _not_ work if `d1.itemsize > d2.itemsize` because `'safe'` implies that you are not okay with losing information when casting from `d1` to `d2`. You are on a 32-bit system and it sounds like there is no 64-bit integer emulation on your OS.

@cpcloud this might be a bug I have to look
period index is backed by int64, by there are conversions necessary at times to platform int
(which should be transparent)

will take a look

@jreback I was just about to ask if this is a numpy or pandas issue :)


Resolving patch:
diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py
--- a/pandas/tseries/period.py
+++ b/pandas/tseries/period.py
@@ -1125,6 +1125,7 @@ def take(self, indices, axis=None):
         """
         Analogous to ndarray.take
         """
+        indices = com._ensure_platform_int(indices)
         taken = self.values.take(indices, axis=axis)
         taken = taken.view(PeriodIndex)
         taken.freq = self.freq


Test patch:
diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py
--- a/pandas/tests/test_groupby.py
+++ b/pandas/tests/test_groupby.py
@@ -318,6 +318,15 @@ def test_agg_period_index(self):
         rs = df.groupby(level=0).sum()
         self.assert_(isinstance(rs.index, PeriodIndex))
 
+        # GH 3579
+        index = period_range(start='1999-01', periods=5, freq='M')
+        s1 = Series(np.random.rand(len(index)), index=index)
+        s2 = Series(np.random.rand(len(index)), index=index)
+        series = [('s1', s1), ('s2',s2)]
+        df = DataFrame.from_items(series)
+        grouped = df.groupby(df.index.month)
+        list(grouped)
+
     def test_agg_must_agg(self):
         grouped = self.df.groupby('A')['C']
         self.assertRaises(Exception, grouped.agg, lambda x: x.describe())