Skip to content

BUG: scalar getitem indexing with CategoricalIndex #16131

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 Apr 25, 2017 · 0 comments · Fixed by #16123
Closed

BUG: scalar getitem indexing with CategoricalIndex #16131

jorisvandenbossche opened this issue Apr 25, 2017 · 0 comments · Fixed by #16123
Labels
Bug Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@jorisvandenbossche
Copy link
Member

Scalar __getitem__ indexing with a CategoricalIndex gives a KeyError (example with two different types of categoricals):

In [46]: cats = pd.Categorical(['a', 'b'])

In [47]: s = pd.Series([1, 2], index=cats)

In [48]: s[cats[0]]
...
KeyError: 'a'

In [49]: s.loc[cats[0]]
Out[49]: 1

In [50]: cats = pd.Categorical([pd.Timestamp('12-31-1999'),pd.Timestamp('12-31-2000')])

In [51]: s = pd.Series([1, 2], index=cats)

In [52]: s[cats[0]]
...
KeyError: Timestamp('1999-12-31 00:00:00')

In [53]: s.loc[cats[0]]
Out[53]: 1

so with loc it works correctly, as well as when passing a list to [] (after #16115 is fixed)

__getitem__ on the columns works correctly:

In [55]: df = pd.DataFrame([[1, 2], [3, 4]], columns=cats)

In [56]: df[cats[0]]
Out[56]: 
0    1
1    3
Name: 1999-12-31 00:00:00, dtype: int64
@jorisvandenbossche jorisvandenbossche added Bug Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves labels Apr 25, 2017
@jreback jreback added this to the 0.20.0 milestone Apr 26, 2017
jreback added a commit that referenced this issue Apr 26, 2017
* REGR: Bug in indexing with a CategoricalIndex

closes #16115

* some cleaning

* BUG: scalar getitem with a CI

closes #16131
pcluo pushed a commit to pcluo/pandas that referenced this issue May 22, 2017
* REGR: Bug in indexing with a CategoricalIndex

closes pandas-dev#16115

* some cleaning

* BUG: scalar getitem with a CI

closes pandas-dev#16131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants