Skip to content

Can't access multi-index names when iterating over rows #35538

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
konstantinmiller opened this issue Aug 4, 2020 · 1 comment
Closed

Can't access multi-index names when iterating over rows #35538

konstantinmiller opened this issue Aug 4, 2020 · 1 comment
Labels
Bug Duplicate Report Duplicate issue or pull request

Comments

@konstantinmiller
Copy link

When iterating over rows with either iterrows() or itertuples(), it is not possible to access index names if you have a multi-index.

So, I would expect something like that to work

import pandas as pd

df = pd.DataFrame(
    index=pd.MultiIndex(
        names=['ind1'],
        levels=[['a']],
        codes=[[0]]
    ),
    data={'C': [42]})

ind, row = next(df.iterrows())
row.C
ind.ind1

However, since ind is not a named tuple, it throws an AttributeError: 'tuple' object has no attribute 'ind1'

Same with itertuples():

import pandas as pd

df = pd.DataFrame(
    index=pd.MultiIndex(
        names=['ind1'],
        levels=[['a']],
        codes=[[0]]
    ),
    data={'C': [42]})

row = next(df.itertuples())
ind = row.Index
row.C
ind.ind1

throws AttributeError: 'tuple' object has no attribute 'ind1'

@konstantinmiller konstantinmiller added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2020
@simonjayhawkins
Copy link
Member

Thanks @konstantinmiller for the report. closing as duplicate of #27407. you may want to add a comment there to maybe include iterrows in that discussion.

@simonjayhawkins simonjayhawkins added Duplicate Report Duplicate issue or pull request and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 4, 2020
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
Projects
None yet
Development

No branches or pull requests

2 participants