Skip to content

Can't .loc[label] on a CategoricalIndex with labels being integer #17569

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
aavanian opened this issue Sep 18, 2017 · 5 comments · Fixed by #29922
Closed

Can't .loc[label] on a CategoricalIndex with labels being integer #17569

aavanian opened this issue Sep 18, 2017 · 5 comments · Fixed by #29922
Labels
Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@aavanian
Copy link

I came around this and this seemed related to #9595 but could also be a bug in the above interacting with the CategoricalIndex. Using the same example as #15470 with pandas 0.20.3:

Code Sample

    s = pd.Series([2, 1, 0], index=pd.CategoricalIndex([2, 1, 0]))
    s[2]  # works (interpreting as label)
    s.loc[2]  # fails with TypeError: cannot do label indexing on <class 'pandas.core.indexes.category.CategoricalIndex'> with these indexers [2] of <class 'int'>

    # of course the below works!
    s = pd.Series([2, 1, 0], index=[2, 1, 0])
    s[2]  # works (interpreting as label)
    s.loc[2]  # works (interpreting as label)

Problem description

s.loc[2] shouldn't fail and should select the label '2'.
Note this happens also with a pd.DataFrame.

@jreback
Copy link
Contributor

jreback commented Sep 18, 2017

duplicate of #15470

@jreback jreback closed this as completed Sep 18, 2017
@jreback jreback added Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves labels Sep 18, 2017
@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Sep 18, 2017
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Sep 18, 2017
@jreback
Copy link
Contributor

jreback commented Sep 18, 2017

why don't you just show this example there (IOW your top section). its the same issue.

@jorisvandenbossche jorisvandenbossche removed the Duplicate Report Duplicate issue or pull request label Sep 18, 2017
@jorisvandenbossche
Copy link
Member

Actually, this is about the TypeError that loc raises, not about __getitem__

@jreback
Copy link
Contributor

jreback commented Sep 18, 2017

and its the exact same issue

@jorisvandenbossche
Copy link
Member

It's clearly not the same user facing issue, so I suppose you mean "it probably has the same underlying reason/bug for this failure" ? If so, can you give a bit more explanation?

The other issue is not necessarily a bug (although if we decide not to be a bug, very confusing behaviour), it is a API design question where we have to ask whether a categorical index with numerical indices is actually a numerical index or not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
3 participants