BUG: MultiIndex alignment solved for rows but not columns · Issue #60498 · pandas-dev/pandas (original) (raw)

Pandas version checks

Issue Description

Hi the alignment of row MultiIndex was solved with #54875 thank you,
but it could it please work for a column MultiIndex as well. The same AssertionError: Length of new_levels (2) must be <= self.nlevels (1) still happens for the 2nd half of my example.

Reproducible Example

Adjusted from https://github.com/pandas-dev/pandas/issues/54875

that has been solved for row MultiIndex.

Would like it to work for column MultiIndex as well.

import pandas as pd import numpy as np

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

This part is just to create the example dataframes. It could be more elegant.

Lev1L = ["1A", "1B"] Lev2L = ["2A", "2B"] rix1 = pd.MultiIndex.from_product([Lev1L, Lev2L], names=["Lev1", "Lev2"]) rix2 = pd.MultiIndex.from_product([Lev1L], names=["Lev1"]) ColL = ["C1", "C2"] df1 = pd.DataFrame(data=100, index=rix1, columns=ColL) df2 = pd.DataFrame(data=np.array([[0.1, 0.2], [0.25, 0.45]]), index=rix2, columns=ColL) df1.columns.name = "Lev10" df2.columns.name = "Lev10" df1t = df1.T df2t = df2.T

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

ROW alignment .. works

print("\n" + "-" * 10 + " ROW MultiIndex\n") print(df1) print() print(df2) df3 = df1.mul(df2) print() print(df3)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

COL alignment .. fails

print("\n" + "-" * 10 + " COL MultiIndex\n") print(df1t) print() print(df2t)

df3t = df1t.mul(df2t) # AssertionError: Length of new_levels (2) must be <= self.nlevels (1) print() print(df3t)

""" ---------- ROW MultiIndex

Lev10 C1 C2 Lev1 Lev2 1A 2A 100 100 2B 100 100 1B 2A 100 100 2B 100 100

Lev10 C1 C2 Lev1 1A 0.10 0.20 1B 0.25 0.45

Lev10 C1 C2 Lev1 Lev2 1A 2A 10.0 20.0 2B 10.0 20.0 1B 2A 25.0 45.0 2B 25.0 45.0

---------- COL MultiIndex

Lev1 1A 1B Lev2 2A 2B 2A 2B Lev10 C1 100 100 100 100 C2 100 100 100 100

Lev1 1A 1B Lev10 C1 0.1 0.25 C2 0.2 0.45 """

Expected Behavior

""" Lev1 1A 1B Lev2 2A 2B 2A 2B Lev10 C1 10.0 10.0 25.0 25.0 C2 20.0 20.0 45.0 45.0 """

Installed versions

commit : https://github.com/pandas-dev/pandas/commit/a36c44e129bd2f70c25d5dec89cb2893716bdbf6
python : 3.12.6
python-bits : 64
OS : Windows
OS-release : 11
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_South Africa.1252

pandas : 3.0.0.dev0+1757.ga36c44e129
numpy : 2.1.3
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pytz : 2024.2
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None