Skip to content

BUG: reindex on level of multi-index does not respect order of new index ? #8241

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

Closed
jorisvandenbossche opened this issue Sep 11, 2014 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@jorisvandenbossche
Copy link
Member

When having a MultiIndex, and reindexing on a level of it, reindex does not respect the order of the new index. Is this a bug? Or a limitation of the multi-index reindexing (as in one level you don't have unique elements?) which should be documented?

In [69]: s = pd.Series([0,1,2,3,4,5,6,7], index=pd.MultiIndex.from_product([[1,2,3,4],[7,8]]))

In [70]: s
Out[70]:
1  7    0
   8    1
2  7    2
   8    3
3  7    4
   8    5
4  7    6
   8    7
dtype: int64

In [71]: s.reindex([3,2], level=0)
Out[71]:
2  7    2
   8    3
3  7    4
   8    5
dtype: int64

Als with a dataframe multi-indexed columns:

In [72]: df = pd.DataFrame(np.random.randn(5,6), columns=pd.MultiIndex.from_prod
uct([list('abc'), list('ef')]))

In [73]: df
Out[73]:
          a                   b                   c
          e         f         e         f         e         f
0 -0.968421  0.343670  0.353754  0.211625  1.964620  0.696301
1 -1.082284  0.532646  0.099657 -1.355934 -0.748171 -0.766453
2  0.674002 -0.440444 -0.296365  0.225170  0.592352 -0.691878
3  0.880808  0.470677  0.330382  0.937828 -0.225192 -1.059934
4 -0.616588  0.501665 -0.232722  1.300403  0.235369  2.318810

In [74]: df.reindex(columns=['c', 'b'], level=0)
Out[74]:
          b                   c
          e         f         e         f
0  0.353754  0.211625  1.964620  0.696301
1  0.099657 -1.355934 -0.748171 -0.766453
2 -0.296365  0.225170  0.592352 -0.691878
3  0.330382  0.937828 -0.225192 -1.059934
4 -0.232722  1.300403  0.235369  2.318810
@jreback
Copy link
Contributor

jreback commented Sep 11, 2014

this is a dupe of #4088 (currently an open issue)

@jreback jreback added Bug Reshaping Concat, Merge/Join, Stack/Unstack, Explode MultiIndex Duplicate Report Duplicate issue or pull request labels Sep 11, 2014
@jreback jreback closed this as completed Sep 11, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants