Repository: pandas-dev/pandas

Issue:
PeriodIndex construction with Series (for year, quarter) give TypeError
Example from Wes' book:

```
In [1]: data = pd.DataFrame({'year':[1959,1959,1959,1959,1960,1960,1960,1960], '
quarter':[1,2,3,4,1,2,3,4]})
In [2]: data
Out[2]:
   quarter  year
0        1  1959
1        2  1959
2        3  1959
3        4  1959
4        1  1960
5        2  1960
6        3  1960
7        4  1960

In [3]: pd.PeriodIndex(year=data.year, quarter=data.quarter)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
...
C:\Anaconda\envs\devel\lib\site-packages\numpy\core\fromnumeric.pyc in repeat(a,
 repeats, axis)
    342     except AttributeError:
    343         return _wrapit(a, 'repeat', repeats, axis)
--> 344     return repeat(repeats, axis)
    345
    346

TypeError: repeat() takes exactly 2 arguments (3 given)
```

The docstring of PeriodIndex says `int or array` for the `year` and `quarter` keywords, and indeed with arrays it does work:

```
In [4]: pd.PeriodIndex(year=data.year.values, quarter=data.quarter.values)
Out[4]:
<class 'pandas.tseries.period.PeriodIndex'>
[1959Q1, ..., 1960Q4]
Length: 8, Freq: Q
```

But it previously also worked for Series (I suppose when it was still a subclass?). So, should we keep the current behaviour or make it also work for Series? 
In any case, a very strange error message that should be improved.



Discussion:

Resolving patch:
diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py
--- a/pandas/tseries/period.py
+++ b/pandas/tseries/period.py
@@ -14,7 +14,7 @@
 
 import pandas.core.common as com
 from pandas.core.common import (isnull, _INT64_DTYPE, _maybe_box,
-                                _values_from_object)
+                                _values_from_object, ABCSeries)
 from pandas import compat
 from pandas.lib import Timestamp
 import pandas.lib as lib
@@ -1261,13 +1261,13 @@ def _range_from_fields(year=None, month=None, quarter=None, day=None,
 def _make_field_arrays(*fields):
     length = None
     for x in fields:
-        if isinstance(x, (list, np.ndarray)):
+        if isinstance(x, (list, np.ndarray, ABCSeries)):
             if length is not None and len(x) != length:
                 raise ValueError('Mismatched Period array lengths')
             elif length is None:
                 length = len(x)
 
-    arrays = [np.asarray(x) if isinstance(x, (np.ndarray, list))
+    arrays = [np.asarray(x) if isinstance(x, (np.ndarray, list, ABCSeries))
               else np.repeat(x, length) for x in fields]
 
     return arrays


Test patch:
diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py
--- a/pandas/tseries/tests/test_period.py
+++ b/pandas/tseries/tests/test_period.py
@@ -1281,6 +1281,15 @@ def test_constructor_nat(self):
         self.assertRaises(
             ValueError, period_range, start='2011-01-01', end='NaT', freq='M')
 
+    def test_constructor_year_and_quarter(self):
+        year = pd.Series([2001, 2002, 2003])
+        quarter = year - 2000
+        idx = PeriodIndex(year=year, quarter=quarter)
+        strs = ['%dQ%d' % t for t in zip(quarter, year)]
+        lops = list(map(Period, strs))
+        p = PeriodIndex(lops)
+        tm.assert_index_equal(p, idx)
+
     def test_is_(self):
         create_index = lambda: PeriodIndex(freq='A', start='1/1/2001',
                                            end='12/1/2009')