Skip to content

Commit a65893a

Browse files
Licht-Tgfyoung
authored andcommitted
BUG: Fix combine_first converts other columns type into floats unexpectedly
1 parent f771ef6 commit a65893a

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

pandas/core/frame.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5073,14 +5073,24 @@ def combine(self, other, func, fill_value=None, overwrite=True):
50735073
series[this_mask] = fill_value
50745074
otherSeries[other_mask] = fill_value
50755075

5076-
# if we have different dtypes, possibly promote
5077-
new_dtype = this_dtype
5078-
if not is_dtype_equal(this_dtype, other_dtype):
5079-
new_dtype = find_common_type([this_dtype, other_dtype])
5080-
if not is_dtype_equal(this_dtype, new_dtype):
5076+
if col not in self.columns:
5077+
# If self DataFrame does not have col in other DataFrame,
5078+
# try to promote series, which is all NaN, as other_dtype.
5079+
new_dtype = other_dtype
5080+
try:
50815081
series = series.astype(new_dtype)
5082-
if not is_dtype_equal(other_dtype, new_dtype):
5083-
otherSeries = otherSeries.astype(new_dtype)
5082+
except ValueError:
5083+
# e.g. new_dtype is integer types
5084+
pass
5085+
else:
5086+
# if we have different dtypes, possibly promote
5087+
new_dtype = this_dtype
5088+
if not is_dtype_equal(this_dtype, other_dtype):
5089+
new_dtype = find_common_type([this_dtype, other_dtype])
5090+
if not is_dtype_equal(this_dtype, new_dtype):
5091+
series = series.astype(new_dtype)
5092+
if not is_dtype_equal(other_dtype, new_dtype):
5093+
otherSeries = otherSeries.astype(new_dtype)
50845094

50855095
# see if we need to be represented as i8 (datetimelike)
50865096
# try to keep us at this dtype
@@ -5154,6 +5164,11 @@ def combiner(x, y, needs_i8_conversion=False):
51545164
else:
51555165
mask = isna(x_values)
51565166

5167+
# If the column y in other DataFrame is not in first DataFrame,
5168+
# just return y_values.
5169+
if y.name not in self.columns:
5170+
return y_values
5171+
51575172
return expressions.where(mask, y_values, x_values)
51585173

51595174
return self.combine(other, combiner, overwrite=False)

0 commit comments

Comments
 (0)