Skip to content
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

Series with MultiIndex with PeriodIndex can't do unstack() #4342

Closed
ruoyu0088 opened this issue Jul 24, 2013 · 4 comments · Fixed by #7041
Closed

Series with MultiIndex with PeriodIndex can't do unstack() #4342

ruoyu0088 opened this issue Jul 24, 2013 · 4 comments · Fixed by #7041
Labels
Bug MultiIndex Period Period data type Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ruoyu0088
Copy link

Here is the code:

import pandas as pd

idx1 = pd.PeriodIndex(["2013-01", "2013-01", "2013-02", "2013-02"], freq="M")
idx2 = ["a","b","a","b"]
value = [1,2,3,4]

idx = pd.MultiIndex.from_arrays([idx1, idx2])
s = pd.Series(value, index=idx)

s.unstack()

It will raise following exception:

anaconda/lib/python2.7/site-packages/pandas-0.12.0.dev_424e187-py2.7-linux-x86_64.egg/pandas/tseries/period.pyc in _from_arraylike(cls, data, freq, tz)
    649 
    650                 if freq is None:
--> 651                     raise ValueError(('freq not specified and cannot be '
    652                                       'inferred from first element'))
    653 

ValueError: freq not specified and cannot be inferred from first element
@jreback
Copy link
Contributor

jreback commented Jul 24, 2013

marking as a bug

@kevinastone
Copy link
Contributor

Any way to work around this issue?

@McRip2
Copy link

McRip2 commented Sep 5, 2013

What about converting the Period-index back to a timestamp-index before unstacking? Afterwards we can go back to a Period-index.

s.index = pd.MultiIndex.from_tuples([(x[0].to_timestamp(),x[1]) for x in s.index])
s = s.unstack()
s.index = pd.PeriodIndex(list(s.index),freq='M')

@hayd
Copy link
Contributor

hayd commented Jan 26, 2014

@jreback jreback modified the milestones: 0.15.0, 0.14.0 Mar 9, 2014
@jreback jreback modified the milestones: 0.14.0, 0.15.0, 0.14.1 May 5, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex Period Period data type Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants