Skip to content
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

DataFrame.xs("key", axis=1, drop_level=False) still dropping level #19056

boulund opened this issue Jan 3, 2018 · 3 comments

DataFrame.xs("key", axis=1, drop_level=False) still dropping level #19056

boulund opened this issue Jan 3, 2018 · 3 comments


Copy link

@boulund boulund commented Jan 3, 2018

Code Sample, a copy-pastable example if possible

df = pd.DataFrame([[1, 2, 3], [2, 4, 6]], columns=["type1_subtype1_subsubtype1", "type1_subtype1_subsubtype2", "type2_subtype1_subsubtype2"], index=["rowtype_rowsubtype1_rowlevel1", "row_rowlevel0_rowlevel1"])
df.columns = d.columns.str.split("_", expand=True).rename(["Sample_type", "Subtype", "SubSubtype"])
df.index = d.index.str.split("_", expand=True).rename(["Rowtype", "rowsubtype", "rowlevel"])
print("Incorrect behavior:")
print(df.xs("type1", axis=1, drop_level=False))
print("Expected output:")
print(df.transpose().xs("type1", drop_level=False).transpose())

Problem description

I checked if there was a previous issue about this in the issue tracker but my searches gave nothing similar.

It appears that drop_level=False doesn't work as intended when taking a cross section along axis=1. Current workaround is to transpose the dataframe, then do the cross section, and then transpose it back again (as in my example above).

Expected Output

Sample_type                         type1            
Subtype                          subtype1            
SubSubtype                    subsubtype1 subsubtype2
Rowtype rowsubtype  rowlevel                         
rowtype rowsubtype1 rowlevel1           1           2
row     rowlevel0   rowlevel1           2           4

Output of pd.show_versions()


commit: None
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.11.1.el7.x86_64
machine: x86_64
byteorder: little
LC_ALL: None

pandas: 0.20.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 36.5.0.post20170921
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.19.0
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.3.0
numexpr: 2.6.2
feather: 0.3.1
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
pandas_gbq: None
pandas_datareader: None

Copy link

@tothandor tothandor commented Mar 1, 2018

I can confirm that for Pandas version 0.22 (Python 2.7 64-bit).


Copy link

@simonjayhawkins simonjayhawkins commented Dec 21, 2018

#6507 maybe related.

drop_level=False also ignored on index when fully specified tuple..

import pandas as pd
df = pd.DataFrame([[1, 2, 3], [2, 4, 6]], columns=["type1_subtype1_subsubtype1", "type1_subtype1_subsubtype2", "type2_subtype1_subsubtype2"], index=["rowtype_rowsubtype1_rowlevel1", "row_rowlevel0_rowlevel1"])
df.columns = df.columns.str.split("_", expand=True).rename(["Sample_type", "Subtype", "SubSubtype"])
df.index = df.index.str.split("_", expand=True).rename(["Rowtype", "rowsubtype", "rowlevel"])
df.xs(('row','rowlevel0','rowlevel1'), drop_level=False)
Sample_type  Subtype   SubSubtype 
type1        subtype1  subsubtype1    2
                       subsubtype2    4
type2        subtype1  subsubtype2    6
Name: (row, rowlevel0, rowlevel1), dtype: int64


Copy link

@bicarlsen bicarlsen commented Apr 28, 2019

I am having the same issue for Pandas version 0.23.0 (Python v 3.6.5 64-bit)


@jbrockmendel jbrockmendel added this to MultiIndex in Indexing Feb 19, 2020
@jreback jreback added this to the 1.2 milestone Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

6 participants