You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.importpandasaspdimportnumpyasnp# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *# 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.Tdf2t=df2.T# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *# ROW alignment .. worksprint("\n"+"-"*10+" ROW MultiIndex\n")
print(df1)
print()
print(df2)
df3=df1.mul(df2)
print()
print(df3)
# * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *# COL alignment .. failsprint("\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 MultiIndexLev10 C1 C2Lev1 Lev21A 2A 100 100 2B 100 1001B 2A 100 100 2B 100 100Lev10 C1 C2Lev11A 0.10 0.201B 0.25 0.45Lev10 C1 C2Lev1 Lev21A 2A 10.0 20.0 2B 10.0 20.01B 2A 25.0 45.0 2B 25.0 45.0---------- COL MultiIndexLev1 1A 1BLev2 2A 2B 2A 2BLev10C1 100 100 100 100C2 100 100 100 100Lev1 1A 1BLev10C1 0.1 0.25C2 0.2 0.45"""
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
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
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
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
Expected Behavior
Installed versions
The text was updated successfully, but these errors were encountered: