BUG: .mul on multi index columns doesnt work. · Issue #61009 · pandas-dev/pandas (original) (raw)

Pandas version checks

Reproducible Example

import pandas as pd data = pd.DataFrame( { "state": (["vic", "nsw", "tas"] * 3 + ['vic'])*2, "colour": ['red'] * 10 + ['blue'] * 10, "month": ['mar', 'sep'] * 10, "year": [2020, 2020, 2020, 2021, 2021, 2021, 2022, 2022, 2022, 2023] * 2, "value": range(20), } ).set_index(['state','colour','year', 'month']).unstack(['state','year','month'])['value'] data.pipe(print) """ state vic nsw tas vic nsw tas vic nsw tas vic year 2020 2020 2020 2021 2021 2021 2022 2022 2022 2023 month mar sep mar sep mar sep mar sep mar sep colour
blue 10 11 12 13 14 15 16 17 18 19 red 0 1 2 3 4 5 6 7 8 9 """

scaler = pd.DataFrame( [ {"year": 2020, "month": "mar", "scale": 0.5}, {"year": 2020, "month": "sep", "scale": 0.5}, {"year": 2021, "month": "mar", "scale": 0.5}, {"year": 2021, "month": "sep", "scale": 0.5}, {"year": 2022, "month": "mar", "scale": 0.5}, {"year": 2022, "month": "sep", "scale": 0.5}, {"year": 2023, "month": "mar", "scale": 0.5}, {"year": 2023, "month": "sep", "scale": 0.5}, ] ).set_index(['year','month'])['scale'] scaler.pipe(print) """ year month 2020 mar 0.5 sep 0.5 2021 mar 0.5 sep 0.5 2022 mar 0.5 sep 0.5 2023 mar 0.5 sep 0.5 Name: scale, dtype: float64 """

mul_on_cols = data.mul(scaler, axis = 1) mul_on_cols.pipe(print) """ state vic tas nsw vic tas vic tas nsw NaN vic year 2020 2020 2020 2021 2021 2021 2022 2022 2022 2023 2023 month mar mar sep mar sep sep mar mar sep mar sep colour
blue NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN red NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN """

mul_on_index = data.T.mul(scaler, axis = 0).T mul_on_index.pipe(print) """ state vic tas nsw vic tas vic tas nsw NaN vic year 2020 2020 2020 2021 2021 2021 2022 2022 2022 2023 2023 month mar mar sep mar sep sep mar mar sep mar sep colour
blue 5.0 6.0 5.5 7.0 6.5 7.5 8.0 9.0 8.5 NaN 9.5 red 0.0 1.0 0.5 2.0 1.5 2.5 3.0 4.0 3.5 NaN 4.5 """

Issue Description

using .mul on multi index columns fails, but on multi index rows, works as expected. View screenshots and code example.

Expected Behavior

Refer to screenshots and example

Installed Versions

INSTALLED VERSIONS

commit : 0691c5c
python : 3.10.16
python-bits : 64
OS : Linux
OS-release : 5.15.167.4-microsoft-standard-WSL2
Version : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.3
numpy : 1.26.0
pytz : 2024.1
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.17.2
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.13.3
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2025.2.0
html5lib : None
hypothesis : None
gcsfs : 2025.2.0
jinja2 : 3.1.5
lxml.etree : 5.3.0
matplotlib : 3.10.0
numba : 0.61.0
numexpr : None
odfpy : None
openpyxl : 3.1.5
pandas_gbq : None
psycopg2 : 2.9.9
pymysql : None
pyarrow : 17.0.0
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : 1.0.10
s3fs : None
scipy : 1.15.2
sqlalchemy : 2.0.38
tables : None
tabulate : 0.9.0
xarray : 2025.1.2
xlrd : 2.0.1
xlsxwriter : 3.2.2
zstandard : 0.23.0
tzdata : 2025.1
qtpy : None
pyqt5 : None