Skip to content

BUG: Fix MultiIndex alignment issue in Dataframe-Series binary operat… #61017

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

Merged
merged 2 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,7 @@ MultiIndex
- :meth:`MultiIndex.insert` would not insert NA value correctly at unified location of index -1 (:issue:`59003`)
- :func:`MultiIndex.get_level_values` accessing a :class:`DatetimeIndex` does not carry the frequency attribute along (:issue:`58327`, :issue:`57949`)
- Bug in :class:`DataFrame` arithmetic operations in case of unaligned MultiIndex columns (:issue:`60498`)
- Bug in :class:`DataFrame` arithmetic operations with :class:`Series` in case of unaligned MultiIndex (:issue:`61009`)
-

I/O
Expand Down
5 changes: 1 addition & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9636,10 +9636,7 @@ def _align_series(

left = self._constructor_from_mgr(fdata, axes=fdata.axes)

if ridx is None:
right = other.copy(deep=False)
else:
right = other.reindex(join_index, level=level)
right = other._reindex_indexer(join_index, ridx)

# fill
fill_na = notna(fill_value)
Expand Down
37 changes: 37 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,43 @@ def test_frame_multiindex_operations_part_align(self):

tm.assert_frame_equal(result, expected)

def test_frame_multiindex_operations_part_align_axis1(self):
# GH#61009 Test DataFrame-Series arithmetic operation
# with partly aligned MultiIndex and axis = 1
df = DataFrame(
[[1, 2, 3], [3, 4, 5]],
index=[2010, 2020],
columns=MultiIndex.from_tuples(
[
("a", "b", 0),
("a", "b", 1),
("a", "c", 2),
],
names=["scen", "mod", "id"],
),
)

series = Series(
[0.4],
index=MultiIndex.from_product([["b"], ["a"]], names=["mod", "scen"]),
)

expected = DataFrame(
[[1.4, 2.4, np.nan], [3.4, 4.4, np.nan]],
index=[2010, 2020],
columns=MultiIndex.from_tuples(
[
("a", "b", 0),
("a", "b", 1),
("a", "c", 2),
],
names=["scen", "mod", "id"],
),
)
result = df.add(series, axis=1)

tm.assert_frame_equal(result, expected)


class TestFrameArithmetic:
def test_td64_op_nat_casting(self):
Expand Down