Skip to content

Commit 8f802cd

Browse files
authored
BUG: Fix bug in DataFrame binary op not respecting fill_value in case… (#60906)
BUG: Fix bug in DataFrame binary op not respecting fill_value in case of MultiIndex columns
1 parent d14f7cf commit 8f802cd

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pandas/core/frame.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8029,10 +8029,15 @@ def _should_reindex_frame_op(self, right, op, axis: int, fill_value, level) -> b
80298029
return False
80308030

80318031
if (
8032-
isinstance(self.columns, MultiIndex)
8033-
or isinstance(right.columns, MultiIndex)
8034-
) and not self.columns.equals(right.columns):
8032+
(
8033+
isinstance(self.columns, MultiIndex)
8034+
or isinstance(right.columns, MultiIndex)
8035+
)
8036+
and not self.columns.equals(right.columns)
8037+
and fill_value is None
8038+
):
80358039
# GH#60498 Reindex if MultiIndexe columns are not matching
8040+
# GH#60903 Don't reindex if fill_value is provided
80368041
return True
80378042

80388043
if fill_value is None and level is None and axis == 1:

pandas/tests/frame/test_arithmetic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2058,6 +2058,26 @@ def test_arithmetic_multiindex_column_align():
20582058
tm.assert_frame_equal(result, expected)
20592059

20602060

2061+
def test_arithmetic_multiindex_column_align_with_fillvalue():
2062+
# GH#60903
2063+
df1 = DataFrame(
2064+
data=[[1.0, 2.0]],
2065+
columns=MultiIndex.from_tuples([("A", "one"), ("A", "two")]),
2066+
)
2067+
df2 = DataFrame(
2068+
data=[[3.0, 4.0]],
2069+
columns=MultiIndex.from_tuples([("B", "one"), ("B", "two")]),
2070+
)
2071+
expected = DataFrame(
2072+
data=[[1.0, 2.0, 3.0, 4.0]],
2073+
columns=MultiIndex.from_tuples(
2074+
[("A", "one"), ("A", "two"), ("B", "one"), ("B", "two")]
2075+
),
2076+
)
2077+
result = df1.add(df2, fill_value=0)
2078+
tm.assert_frame_equal(result, expected)
2079+
2080+
20612081
def test_bool_frame_mult_float():
20622082
# GH 18549
20632083
df = DataFrame(True, list("ab"), list("cd"))

0 commit comments

Comments
 (0)