Skip to content
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

BUG: MultiIndex alignment solved for rows but not columns #60498

Open
3 tasks done
ZtrevorZ opened this issue Dec 5, 2024 · 3 comments · May be fixed by #60538
Open
3 tasks done

BUG: MultiIndex alignment solved for rows but not columns #60498

ZtrevorZ opened this issue Dec 5, 2024 · 3 comments · May be fixed by #60538
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@ZtrevorZ
Copy link

ZtrevorZ commented Dec 5, 2024

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

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
@ZtrevorZ ZtrevorZ added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 5, 2024
@rhshadrach
Copy link
Member

Thanks for the report! I think the expected output here should be floats and not integers. Further investigations and PRs to fix are welcome.

Please be patient as this is my first bug report.

Shorter is better. Fewer levels, fewer columns if you can reproduce the issue.

@rhshadrach rhshadrach added Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 8, 2024
@ZtrevorZ
Copy link
Author

ZtrevorZ commented Dec 9, 2024

Thank you so much for the comment and also for your development efforts. My example has been edited to remove one level for a shorter script. Yes the output should be floats not integers. Best regards

@snitish
Copy link
Contributor

snitish commented Dec 11, 2024

take

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants