Skip to content

BUG: .loc sometimes does not swap columns in multi-index dataframes #52656

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

Open
3 tasks done
LoicCousin opened this issue Apr 13, 2023 · 1 comment
Open
3 tasks done
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex

Comments

@LoicCousin
Copy link

LoicCousin commented Apr 13, 2023

Pandas version checks

  • I have checked that this issue has not already been reported.

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

*file.csv*
design_parameters;design_parameters;design_parameters;functions;functions;functions
x_local;x_shared;x_shared;c_1;c_2;obj
0;0;1;0;0;0
1.0;4.000000000000002;3.0000000000000004;-14.597213815595694;-12.786069077975327;21.75722730061834
2.6642724137992957e-09;2.5200529286981936;1.5215140258817428e-09;-2.22264559568493;-19.15989414027956;5.39055181426381
--------------------------------------------------------------
import pandas as pd
df = pd.read_csv("file.csv", delimiter=";", header=[0, 1, 2])

# This .loc does not swap the columns as expected
# Expecting obj before x_shared
# Happens: x_shared before obj
df.loc[:, (slice(None), ["obj", "x_shared"], slice(None))]

# This .loc does swap the columns as expected
# Expected and happened: obj before x_shared before c_1
df.loc[:, (slice(None), ["obj", "x_shared", "c_1"], slice(None))]

Issue Description

Hey,

With some multi-index DataFrames, the .loc method does not seem to swap the columns as expected.
Sometimes it does. Sometimes it does not.

A small exemple that shows the issue can be found above. The .csv I wrote down is a 3 level column indexed dataframe, with only 2 rows. There are only 6 columns. When using .loc, and according to the names I put in the second level (level=1) of multi-indexing, I have different behaviours.
It can be tricky, especially when you use the .loc method just before converting to numpy, as I do.

I've tested with pandas v1.5.1, v1.4 and v2.0.0. Same behaviour.

Here is a behaviour capture:
image

I probably miss something here. Might not be a bug, but I did not find any information on that matter, except an old Issue #22797 (comment).

Thanks for your help,

Loïc

Expected Behavior

import pandas as pd
df = pd.read_csv("file.csv", delimiter=";", header=[0, 1, 2])
df.loc[:, (slice(None), ["obj", "x_shared"], slice(None))]

obj column before x_shared

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.10.10.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : fr_FR.cp1252

pandas : 1.5.1
numpy : 1.23.4
pytz : 2023.3
dateutil : 2.8.2
setuptools : 67.5.1
pip : 23.0.1
Cython : None
pytest : 7.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.6.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : 2023.3

@LoicCousin LoicCousin added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 13, 2023
@topper-123
Copy link
Contributor

Hi @LoicCousin, thanks for the bug report.

You are right, in that case the columns should have been swapped, we swap generally, when indexing using lists. Interesting that it works correctly, when adding the last level.

PRs welcome.

@topper-123 topper-123 added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex
Projects
None yet
Development

No branches or pull requests

2 participants