Skip to content

Commit 1c28ab4

Browse files
Fix for issue pandas-dev#55509
Preserve dtype when updating from dataframe whose NA values don't affect original. I don't know the best place to put the test case in the tests/frame or the tests/dtype directory. Signed-off-by: Michael Tiemann <[email protected]>
1 parent 746e5ee commit 1c28ab4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pandas/core/frame.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
construct_1d_arraylike_from_scalar,
8282
construct_2d_arraylike_from_scalar,
8383
find_common_type,
84+
find_result_type,
8485
infer_dtype_from_scalar,
8586
invalidate_string_dtypes,
8687
maybe_box_native,
@@ -8870,7 +8871,15 @@ def update(
88708871
if mask.all():
88718872
continue
88728873

8873-
self.loc[:, col] = expressions.where(mask, this, that)
8874+
col_dtype = self[col].dtype
8875+
update_result = expressions.where(mask, this, that)
8876+
# Preserve dtype if udpate_result is all compatible with dtype
8877+
if col_dtype != object and update_result.dtype == object:
8878+
if all(
8879+
col_dtype == find_result_type(col_dtype, x) for x in update_result
8880+
):
8881+
update_result = update_result.astype(col_dtype)
8882+
self.loc[:, col] = update_result
88748883

88758884
# ----------------------------------------------------------------------
88768885
# Data reshaping

0 commit comments

Comments
 (0)