Skip to content

PeriodIndex construction with Series (for year, quarter) give TypeError #7701

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions