Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[x] I have confirmed this bug exists on the main branch of pandas.
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
Comment From: snitish
I'm able to reproduce this bug on main. The issue seems to be in this line - https://github.com/pandas-dev/pandas/blob/5da9eb726c915412928ee44a0631a74bda614556/pandas/core/generic.py#L9642
.reindex(join_index, level=level)
is unable to reindex if join_index
is a MultiIndex
with partially different levels. I think it should be using ._reindex_indexer(join_index, ridx)
as we already have the ridx
object. Will work on a fix.
Comment From: snitish
take