Skip to content

Commit a1786ba

Browse files
BUG: update with duplicate frame index pandas-dev#55509
1 parent bb41261 commit a1786ba

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

pandas/core/frame.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -8858,23 +8858,27 @@ def update(
88588858
if not isinstance(other, DataFrame):
88598859
other = DataFrame(other)
88608860

8861-
indexes_intersection = self.index.intersection(other.index)
8861+
indexes_intersection = other.index.intersection(self.index) # order is important
88628862
if not len(indexes_intersection):
88638863
raise ValueError(
8864-
"Can't update dataframe when other has no element in common."
8864+
"Can't update dataframe when other has no index in common with this dataframe."
8865+
)
8866+
8867+
if other.index.is_unique:
8868+
indexes_this = indexes_intersection
8869+
if self.index.is_unique:
8870+
indexes_that = indexes_intersection
8871+
else:
8872+
full_indexes_this = self.index.take(self.index.get_indexer_for(indexes_intersection))
8873+
indexes_that = indexes_intersection.reindex(full_indexes_this)[0]
8874+
else:
8875+
raise ValueError(
8876+
"Update not allowed with duplicate indexes on other."
88658877
)
88668878

88678879
for col in self.columns.intersection(other.columns):
8868-
this = self.loc[indexes_intersection, col]
8869-
that = other.loc[indexes_intersection, col]
8870-
8871-
if this.index.has_duplicates or that.index.has_duplicates:
8872-
raise ValueError(
8873-
"Update not allowed with duplicate indexes on dataframe or other."
8874-
)
8875-
8876-
this = this._values
8877-
that = that._values
8880+
this = self.loc[indexes_this, col]._values
8881+
that = other.loc[indexes_that, col]._values
88788882

88798883
if filter_func is not None:
88808884
mask = ~filter_func(this) | isna(that)

0 commit comments

Comments
 (0)