Skip to content

Commit a2a74ef

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

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

pandas/core/frame.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -5072,14 +5072,24 @@ def combine(self, other, func, fill_value=None, overwrite=True):
50725072
series[this_mask] = fill_value
50735073
otherSeries[other_mask] = fill_value
50745074

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

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

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

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

0 commit comments

Comments
 (0)