Skip to content

Commit c81ce78

Browse files
committed
BUG: Fix combine_first converts other columns type into floats unexpectedly
1 parent ef019fa commit c81ce78

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
@@ -4870,14 +4870,24 @@ def combine(self, other, func, fill_value=None, overwrite=True):
48704870
series[this_mask] = fill_value
48714871
otherSeries[other_mask] = fill_value
48724872

4873-
# if we have different dtypes, possibly promote
4874-
new_dtype = this_dtype
4875-
if not is_dtype_equal(this_dtype, other_dtype):
4876-
new_dtype = find_common_type([this_dtype, other_dtype])
4877-
if not is_dtype_equal(this_dtype, new_dtype):
4873+
if col not in self.columns:
4874+
# If self DataFrame does not have col in other DataFrame,
4875+
# try to promote series, which is all NaN, as other_dtype.
4876+
new_dtype = other_dtype
4877+
try:
48784878
series = series.astype(new_dtype)
4879-
if not is_dtype_equal(other_dtype, new_dtype):
4880-
otherSeries = otherSeries.astype(new_dtype)
4879+
except ValueError:
4880+
# e.g. new_dtype is integer types
4881+
pass
4882+
else:
4883+
# if we have different dtypes, possibly promote
4884+
new_dtype = this_dtype
4885+
if not is_dtype_equal(this_dtype, other_dtype):
4886+
new_dtype = find_common_type([this_dtype, other_dtype])
4887+
if not is_dtype_equal(this_dtype, new_dtype):
4888+
series = series.astype(new_dtype)
4889+
if not is_dtype_equal(other_dtype, new_dtype):
4890+
otherSeries = otherSeries.astype(new_dtype)
48814891

48824892
# see if we need to be represented as i8 (datetimelike)
48834893
# try to keep us at this dtype
@@ -4950,6 +4960,11 @@ def combiner(x, y, needs_i8_conversion=False):
49504960
else:
49514961
mask = isna(x_values)
49524962

4963+
# If the column y in other DataFrame is not in first DataFrame,
4964+
# just return y_values.
4965+
if y.name not in self.columns:
4966+
return y_values
4967+
49534968
return expressions.where(mask, y_values, x_values)
49544969

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

0 commit comments

Comments
 (0)