From fcc44d57366eb75b3607c74a0aaeaa9abedfdd94 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 7 Jul 2020 14:37:33 -0500 Subject: [PATCH 1/2] Fixed Series.apply performance regression Set the option once, rather than in the loop. Closes https://github.com/pandas-dev/pandas/issues/35047 --- pandas/core/apply.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 9c223d66b727b..7aa2180bb8347 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -291,9 +291,8 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"] res_index = res_index.take(successes) else: - for i, v in series_gen_enumeration: - - with option_context("mode.chained_assignment", None): + with option_context("mode.chained_assignment", None): + for i, v in series_gen_enumeration: # ignore SettingWithCopy here in case the user mutates results[i] = self.f(v) From 8c41f09fef7004bb97daed3db09e465b66cff2d4 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 8 Jul 2020 06:43:43 -0500 Subject: [PATCH 2/2] copy in loop --- pandas/core/apply.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 7aa2180bb8347..d4be660939773 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -295,11 +295,10 @@ def apply_series_generator(self, partial_result=None) -> Tuple[ResType, "Index"] for i, v in series_gen_enumeration: # ignore SettingWithCopy here in case the user mutates results[i] = self.f(v) - - if isinstance(results[i], ABCSeries): - # If we have a view on v, we need to make a copy because - # series_generator will swap out the underlying data - results[i] = results[i].copy(deep=False) + if isinstance(results[i], ABCSeries): + # If we have a view on v, we need to make a copy because + # series_generator will swap out the underlying data + results[i] = results[i].copy(deep=False) return results, res_index