Skip to content

BUG: .loc indexing not preserving Index type #15166

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
watercrossing opened this issue Jan 19, 2017 · 2 comments · Fixed by #29533
Closed

BUG: .loc indexing not preserving Index type #15166

watercrossing opened this issue Jan 19, 2017 · 2 comments · Fixed by #29533
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Milestone

Comments

@watercrossing
Copy link
Contributor

test = pd.DataFrame(data=np.arange(2,22,2), 
             index=pd.MultiIndex(levels=[pd.CategoricalIndex(["a", "b"]), range(10)],
                                 labels=[[0]*5 + [1]*5, range(10)],
                                 names = ["Index1", "Index2"]))
test.index.levels[0]
# CategoricalIndex([u'a', u'b'], categories=[u'a', u'b'], ordered=False, name=u'Index1', dtype='category')
test.loc[["a"]].index.levels[0]
# Index([u'a'], dtype='object', name=u'Index1')

Problem description

When selecting data through a categorical index, the categorical index is lost.

Expected Output

test.loc[["a"]].index.levels[0]
# CategoricalIndex([u'a'], categories=[u'a', u'b'], ordered=False, name=u'Index1', dtype='category')

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.10.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-431.11.2.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_GB.utf8
LANG: en_GB.utf8
LOCALE: None.None

pandas: 0.19.2
nose: 1.3.7
pip: 9.0.1
setuptools: 32.3.1
Cython: None
numpy: 1.11.3
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: 1.5.1
patsy: None
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.9999999
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.9.4
boto: None
pandas_datareader: None

@jreback jreback changed the title dataframe loc selector changes CategoricalIndex to Index BUG: .loc indexing not preserving Index type Jan 19, 2017
@jreback
Copy link
Contributor

jreback commented Jan 19, 2017

xref #15163 (comment)

@jreback jreback added Bug Categorical Categorical Data Type Difficulty Intermediate Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Jan 19, 2017
@jreback jreback added this to the 0.20.0 milestone Jan 19, 2017
@jreback jreback modified the milestones: 0.20.0, 0.21.0 Mar 23, 2017
@jreback jreback modified the milestones: 0.21.0, 1.0 Oct 2, 2017
@mroeschke
Copy link
Member

This looks to be fixed on master. Could use a test.


In [245]: test.loc[["a"]].index.levels[0]
     ...:
Out[245]: CategoricalIndex(['a', 'b'], categories=['a', 'b'], ordered=False, dtype='category')

In [246]: pd.__version__
Out[246]: '0.26.0.dev0+593.g9d45934af'

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug Categorical Categorical Data Type Difficulty Intermediate Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex labels Oct 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants