Skip to content

Commit 0f63a26

Browse files
phoflim-vinicius
authored and
im-vinicius
committed
BUG: combine_first ignoring others columns if other is empty (pandas-dev#53792)
* BUG: combine_first ignoring others columns if other is empty * Fix
1 parent 7cf6047 commit 0f63a26

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ Reshaping
491491
- Bug in :func:`merge_asof` raising ``KeyError`` for extension dtypes (:issue:`52904`)
492492
- Bug in :func:`merge_asof` raising ``ValueError`` for data backed by read-only ndarrays (:issue:`53513`)
493493
- Bug in :meth:`DataFrame.agg` and :meth:`Series.agg` on non-unique columns would return incorrect type when dist-like argument passed in (:issue:`51099`)
494+
- Bug in :meth:`DataFrame.combine_first` ignoring other's columns if ``other`` is empty (:issue:`53792`)
494495
- Bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax`, where the axis dtype would be lost for empty frames (:issue:`53265`)
495496
- Bug in :meth:`DataFrame.merge` not merging correctly when having ``MultiIndex`` with single level (:issue:`52331`)
496497
- Bug in :meth:`DataFrame.stack` losing extension dtypes when columns is a :class:`MultiIndex` and frame contains mixed dtypes (:issue:`45740`)

pandas/core/frame.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -8344,7 +8344,13 @@ def combiner(x, y):
83448344

83458345
return expressions.where(mask, y_values, x_values)
83468346

8347-
combined = self.combine(other, combiner, overwrite=False)
8347+
if len(other) == 0:
8348+
combined = self.reindex(
8349+
self.columns.append(other.columns.difference(self.columns)), axis=1
8350+
)
8351+
combined = combined.astype(other.dtypes)
8352+
else:
8353+
combined = self.combine(other, combiner, overwrite=False)
83488354

83498355
dtypes = {
83508356
col: find_common_type([self.dtypes[col], other.dtypes[col]])

pandas/tests/frame/methods/test_combine_first.py

+8
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,11 @@ def test_midx_losing_dtype():
538538
)
539539
expected = DataFrame({"a": [np.nan, 4, 3, 3]}, index=expected_midx)
540540
tm.assert_frame_equal(result, expected)
541+
542+
543+
def test_combine_first_empty_columns():
544+
left = DataFrame(columns=["a", "b"])
545+
right = DataFrame(columns=["a", "c"])
546+
result = left.combine_first(right)
547+
expected = DataFrame(columns=["a", "b", "c"])
548+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)