Skip to content

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

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
1 task
mosalx opened this issue Dec 21, 2020 · 2 comments
Closed
1 task
Labels
Bug MultiIndex Needs Tests Unit test(s) needed to prevent regressions

Comments

@mosalx
Copy link

mosalx commented Dec 21, 2020

  • [ 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

@mosalx mosalx added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 21, 2020
@simonjayhawkins
Copy link
Member

@mosalx Thanks for the report. this appears fixed in master. PR adding tests welcome (if the commit that fixed did not add tests)

>>> pd.__version__
'1.2.0rc0'
>>>
>>> df_unsorted.loc[:, ("level_0", ["a", "b"])]
  level_0
        a  b
0       0  1
1       0  1
2       0  1
>>>
>>> df_unsorted.loc[:, (["level_0"], ["a", "b"])]
  level_0
        a  b
0       0  1
1       0  1
2       0  1
>>>

@simonjayhawkins simonjayhawkins added MultiIndex Needs Tests Unit test(s) needed to prevent regressions and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 21, 2020
@simonjayhawkins simonjayhawkins added this to the Contributions Welcome milestone Dec 21, 2020
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Dec 22, 2020
@simonjayhawkins
Copy link
Member

fixed in commit: [15b8898] Bug in DataFrame.loc returning elements in wrong order when indexer is differently ordered than object (#37992)

closing as tests were added.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug MultiIndex Needs Tests Unit test(s) needed to prevent regressions
Projects
None yet
Development

No branches or pull requests

2 participants