Skip to content

BUG: Fix bug in DataFrame binary op not respecting fill_value in case… #60906

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 1 commit into from
Feb 10, 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
11 changes: 8 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8029,10 +8029,15 @@ def _should_reindex_frame_op(self, right, op, axis: int, fill_value, level) -> b
return False

if (
isinstance(self.columns, MultiIndex)
or isinstance(right.columns, MultiIndex)
) and not self.columns.equals(right.columns):
(
isinstance(self.columns, MultiIndex)
or isinstance(right.columns, MultiIndex)
)
and not self.columns.equals(right.columns)
and fill_value is None
):
# GH#60498 Reindex if MultiIndexe columns are not matching
# GH#60903 Don't reindex if fill_value is provided
return True

if fill_value is None and level is None and axis == 1:
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,26 @@ def test_arithmetic_multiindex_column_align():
tm.assert_frame_equal(result, expected)


def test_arithmetic_multiindex_column_align_with_fillvalue():
# GH#60903
df1 = DataFrame(
data=[[1.0, 2.0]],
columns=MultiIndex.from_tuples([("A", "one"), ("A", "two")]),
)
df2 = DataFrame(
data=[[3.0, 4.0]],
columns=MultiIndex.from_tuples([("B", "one"), ("B", "two")]),
)
expected = DataFrame(
data=[[1.0, 2.0, 3.0, 4.0]],
columns=MultiIndex.from_tuples(
[("A", "one"), ("A", "two"), ("B", "one"), ("B", "two")]
),
)
result = df1.add(df2, fill_value=0)
tm.assert_frame_equal(result, expected)


def test_bool_frame_mult_float():
# GH 18549
df = DataFrame(True, list("ab"), list("cd"))
Expand Down
Loading