Description
The Problem
The nlargest
/ nsmallest
functionality is broken when applied to dataframes with MultiIndex
columns. For example:
columns = pd.MultiIndex.from_tuples([('x', 'a'), ('x', 'b'), ('y', 'c'), ('y', 'd')])
values = np.random.rand(5, 4)
df = pd.DataFrame(data=values, columns=columns)
df.nlargest(3, ('x', 'a'))
This raises an error because the multi-level lookup ('x', 'a')
is interpreted as a list of column names ['x', 'a']
.
This is a problem, because this behavior is inconsistent with the usual multi-level lookup behavior.
Output of pd.show_versions()
pandas: 0.23.4
pytest: None
pip: 10.0.1
setuptools: 39.0.1
Cython: None
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.5.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2018.5
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 3.0.0
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None