diff --git a/pandas/core/frame.py b/pandas/core/frame.py index f7f296e822e15..c0e0c2a464a1d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3503,15 +3503,18 @@ def combine(self, other, func, fill_value=None): ------- result : DataFrame """ - if other.empty: - return self.copy() - if self.empty: - return other.copy() + other_idxlen = len(other.index) # save for compare this, other = self.align(other, copy=False) new_index = this.index + if other.empty and len(new_index) == len(self.index): + return self.copy() + + if self.empty and len(other) == other_idxlen: + return other.copy() + # sorts if possible new_columns = this.columns.union(other.columns) do_fill = fill_value is not None diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index fea84f5a86e36..7f6ee00785501 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -6131,6 +6131,9 @@ def test_combine_first(self): comb = self.empty.combine_first(self.frame) assert_frame_equal(comb, self.frame) + comb = self.frame.combine_first(DataFrame(index=["faz","boo"])) + self.assertTrue("faz" in comb.index) + def test_combine_first_mixed_bug(self): idx = Index(['a','b','c','e']) ser1 = Series([5.0,-9.0,4.0,100.],index=idx)