Skip to content

On Series with CategoricalIndex, __getitem__ not equal to .loc #15470

Closed
@kernc

Description

Code Sample, a copy-pastable example if possible

>>> s = pd.Series([2, 1, 0], index=pd.CategoricalIndex([2, 1, 0]))
>>> s.index
CategoricalIndex([2, 1, 0], categories=[0, 1, 2], ordered=False, dtype='category')
>>> s[[0, 1, 2]].equals(s.loc[[0, 1, 2]])  # Should be True
False
>>> s[[0, 1, 2]].equals(s.iloc[[0, 1, 2]])  # Should be False
True

Problem description

For Series with CategoricalIndex, __getitem__ indexing behaves differently than .loc. On other-indexed series, these accessors return the same result:

>>> s = pd.Series([2, 1, 0], index=[2, 1, 0])
>>> s[[0, 1, 2]].equals(s.loc[[0, 1, 2]])  # Should be True
True
>>> s[[0, 1, 2]].equals(s.iloc[[0, 1, 2]])  # Should be True
False

Expected Output

>>> s = pd.Series([2, 1, 0], index=pd.CategoricalIndex([2, 1, 0]))
>>> s[[0, 1, 2]].equals(s.loc[[0, 1, 2]])
True

Output of pd.show_versions()

0.19.0+479.git

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeDocsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions