Skip to content

Commit 84efff6

Browse files
committed
ENH: try to preserve the dtype on combine_first for the case where the two DataFrame objects have the same columns
1 parent 8fe3dc6 commit 84efff6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pandas/core/frame.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6482,6 +6482,37 @@ def combiner(x, y):
64826482

64836483
return expressions.where(mask, y_values, x_values)
64846484

6485+
if self.columns.equals(other.columns):
6486+
6487+
if self.empty and len(other) == other_idxlen:
6488+
return other.copy()
6489+
if self.index.equals(other.index):
6490+
return self.copy()
6491+
elif self.empty:
6492+
return other.copy()
6493+
elif other.empty:
6494+
return self.copy()
6495+
else:
6496+
new_index = self.index.union(other.index)
6497+
self_index = self.index
6498+
diff_index = other.index.difference(self.index)
6499+
6500+
columns = {}
6501+
6502+
for col in self.columns:
6503+
this_vals = self[col]
6504+
other_vals = other[col][diff_index]
6505+
new_dtype = find_common_type([this_vals.dtype, other_vals.dtype])
6506+
6507+
series = Series([this_vals.values[0]] * len(new_index), dtype=new_dtype, index=new_index)
6508+
6509+
series[self_index] = this_vals
6510+
series[diff_index] = other_vals
6511+
6512+
columns[col] = series
6513+
6514+
return DataFrame.from_dict(columns)
6515+
64856516
return self.combine(other, combiner, overwrite=False)
64866517

64876518
def update(

0 commit comments

Comments
 (0)