Skip to content

Commit 02c1b77

Browse files
BUG: fix dataframe.update not preserving dtypes (pandas-dev#55509)
1 parent d5a1085 commit 02c1b77

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

pandas/core/frame.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -8764,11 +8764,22 @@ def update(
87648764
if not isinstance(other, DataFrame):
87658765
other = DataFrame(other)
87668766

8767-
other = other.reindex(self.index)
8767+
if other.index.has_duplicates:
8768+
raise ValueError("Update not allowed with duplicate indexes on other.")
8769+
8770+
rows = other.index.intersection(self.index)
8771+
if rows.empty:
8772+
raise ValueError(
8773+
"Can't update dataframe when other has no index in common with "
8774+
"this dataframe."
8775+
)
8776+
8777+
other = other.reindex(rows)
8778+
this_data = self.loc[rows]
87688779

87698780
for col in self.columns.intersection(other.columns):
8770-
this = self[col]._values
8771-
that = other[col]._values
8781+
this = this_data[col]
8782+
that = other[col]
87728783

87738784
if filter_func is not None:
87748785
mask = ~filter_func(this) | isna(that)
@@ -8788,7 +8799,7 @@ def update(
87888799
if mask.all():
87898800
continue
87908801

8791-
self.loc[:, col] = self[col].where(mask, that)
8802+
self.loc[rows, col] = this.where(mask, that)
87928803

87938804
# ----------------------------------------------------------------------
87948805
# Data reshaping

0 commit comments

Comments
 (0)