Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Remove for loop from DataFrame.update() #56303

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -8923,29 +8923,29 @@ def update(

other = other.reindex(self.index)

for col in self.columns.intersection(other.columns):
this = self[col]._values
that = other[col]._values
cols = self.columns.intersection(other.columns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling ._values on multiple columns converts to a common dtype for all columns, probably object as soon as you have non-numeric columns. This is terrible from a performance perspective, so doing this at once is actually a lot slower in real world use cases than doing one column at a time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, understood. Thank you for taking the time to explain.

this = self[cols]._values
that = other[cols]._values

if filter_func is not None:
mask = ~filter_func(this) | isna(that)
if filter_func is not None:
mask = ~filter_func(this) | isna(that)
else:
if errors == "raise":
mask_this = notna(that)
mask_that = notna(this)
if (mask_this & mask_that).any():
raise ValueError("Data overlaps.")

if overwrite:
mask = isna(that)
else:
if errors == "raise":
mask_this = notna(that)
mask_that = notna(this)
if any(mask_this & mask_that):
raise ValueError("Data overlaps.")

if overwrite:
mask = isna(that)
else:
mask = notna(this)
mask = notna(this)

# don't overwrite columns unnecessarily
if mask.all():
continue
# no update necessary
if mask.all():
return

self.loc[:, col] = self[col].where(mask, that)
self.loc[:, cols] = self[cols].where(mask, that)

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