Skip to content

BUG: Selection with Multiindex columns may not honor requested column order #38602

Closed
@mosalx

Description

@mosalx
  • [ x] I have checked that this issue has not already been reported.

  • [x ] I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

data = np.broadcast_to(np.arange(2)[None, :], (3, 2))
df_sorted = pd.DataFrame(data, columns=pd.MultiIndex.from_product([['level_0'], ['a', 'b']]), dtype=int)
df_unsorted = df_sorted.reindex(pd.MultiIndex.from_product([['level_0'], ['b', 'a']]), axis=1)
df_unsorted
#   level_0   
#         b  a
# 0       1  0
# 1       1  0
# 2       1  0

df_unsorted.loc[:, ('level_0', ['a', 'b'])]
#   level_0   
#         b  a
# 0       1  0
# 1       1  0
# 2       1  0

df_unsorted.loc[:, (['level_0'], ['a', 'b'])]
#   level_0   
#         a  b
# 0       0  1
# 1       0  1
# 2       0  1

Problem description

Advanced indexing with hierarchical index guide documents a use case when a tuple of lists is passed to the indexer.
When the first tuple element is a column label (not wrapped into a list), the indexer ignores the order of labels specified in the second list (it appears to use the same order in which the labels are present in the column index). Understandably, this usage is not documented (as far as I know) but appears logical. Yet such subtle behavior difference is confusing.
Please consider making this behavior consistent or raise an error when the container passed to 'loc' does not match the documented structure (list of tuples/tuple of lists).

Note: the described behavior is the same for pandas versions all the way back to 0.23.2 (I checked 0.23.2, 0.25.3, 1.0.5, 1.0.0 and 1.1.5)

Expected Output

both examples are expected to output

#   level_0   
#         a  b
# 0       0  1
# 1       0  1
# 2       0  1

OR

df_unsorted.loc[:, ('level_0', ['a', 'b'])] is expected to raise an error (unexpected label structure)

Output of pd.show_versions()

INSTALLED VERSIONS

commit : b5958ee
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 1.1.5
numpy : 1.19.2
pytz : 2020.4
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.0.0.post20201207
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions