Skip to content

Commit

Permalink
Fix for issue pandas-dev#55509
Browse files Browse the repository at this point in the history
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 <72577720+MichaelTiemannOSC@users.noreply.github.com>
  • Loading branch information
MichaelTiemannOSC committed Oct 17, 2023
1 parent 746e5ee commit 1c28ab4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
construct_1d_arraylike_from_scalar,
construct_2d_arraylike_from_scalar,
find_common_type,
find_result_type,
infer_dtype_from_scalar,
invalidate_string_dtypes,
maybe_box_native,
Expand Down Expand Up @@ -8870,7 +8871,15 @@ def update(
if mask.all():
continue

self.loc[:, col] = expressions.where(mask, this, that)
col_dtype = self[col].dtype
update_result = expressions.where(mask, this, that)
# Preserve dtype if udpate_result is all compatible with dtype
if col_dtype != object and update_result.dtype == object:
if all(
col_dtype == find_result_type(col_dtype, x) for x in update_result
):
update_result = update_result.astype(col_dtype)
self.loc[:, col] = update_result

# ----------------------------------------------------------------------
# Data reshaping
Expand Down

0 comments on commit 1c28ab4

Please sign in to comment.