From 2e55cd2b1731e45ed8cf532aaa64129c88ea5906 Mon Sep 17 00:00:00 2001 From: smij720 Date: Sat, 2 Dec 2023 14:47:30 -0800 Subject: [PATCH] Remove for loop from DataFrame.update() --- pandas/core/frame.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 008c1e0d10ba4..eb85851552d83 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -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) + 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