Skip to content

BUG: reindexing a level in MultiIndex has no effect #39754

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
3 tasks done
attack68 opened this issue Feb 11, 2021 · 1 comment
Closed
3 tasks done

BUG: reindexing a level in MultiIndex has no effect #39754

attack68 opened this issue Feb 11, 2021 · 1 comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@attack68
Copy link
Contributor

attack68 commented Feb 11, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


This is about trying to reindex a MultiIndex level to add keys to avoid a KeyError.
This code could be more minimalist and more generic but the context in which the error arose might be helpful..
When using groupby unnecessary keys are eliminated..

idx = pd.IndexSlice
df = pd.DataFrame([
    ['A', 'cat', 10],
    ['A', 'cat', 20],
    ['B', 'dog', 30],
    ['B', 'cat', 40]
], columns = ['Label', 'Animal', 'Score'])
g = df.groupby(['Label', 'Animal']).agg('sum')
g.index
>>> MultiIndex([('A', 'cat'),
               ('B', 'cat'),
               ('B', 'dog')],
                names=['Label', 'Animal'])
g.loc[idx[:, 'dog'], :]     # <--- WORKS because 'dog' is an element of level 1.

But...

g = df[df['Label']=='A].groupby(['Label', 'Animal']).agg('sum')
g.index
>>> MultiIndex([('A', 'cat')],
                            names=['Label', 'Animal'])
g.loc[idx[:, 'dog'], :]     # <--- KeyError  because 'dog' is NOT an element of level 1.

I tried to solve this with

g2 = g.reindex(pd.Index(['cat', 'dog'], name='Other'), level=1)
g2.index
>>> MultiIndex([('A', 'cat')],
           names=['Label', 'Animal']).     # <-- Reindex did nothing.

The docs for Index.reindex don't mention the level argument or give any examples, the docs for DataFrame.reindex were better but still not actually consistent with the arguments and no multiindex examples. And I tried to find any relevant parts of the guide which deal with reindexing multi indexes but without success.

Now that Indexing with missing keys returns a KeyError is it more important to provide example for these issues? Or have I missed something very simple.

@attack68 attack68 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 11, 2021
@attack68
Copy link
Contributor Author

Im will close this due to more info in #39775

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

1 participant