From 71a2201e4c913daed186cbd01dac68ca297bf405 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:39:26 -0700 Subject: [PATCH 1/4] Avoids comprehension iterators --- pandas/core/internals/managers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 82b88d090f847..4172d1cdd520c 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -419,11 +419,10 @@ def apply( result_blocks: list[Block] = [] # fillna: Series/DataFrame is responsible for making sure value is aligned - aligned_args = {k: kwargs[k] for k in align_keys} - for b in self.blocks: - if aligned_args: - for k, obj in aligned_args.items(): + if align_keys: + for k in align_keys: + obj = kwargs[k] if isinstance(obj, (ABCSeries, ABCDataFrame)): # The caller is responsible for ensuring that # obj.axes[-1].equals(self.items) From c0ca2750bc894aa7cbfb198b6238335ed17b924d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 4 Jun 2024 11:32:06 -0700 Subject: [PATCH 2/4] Avoid reiterating --- pandas/core/apply.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 33f506235870d..3d9f3758b0cd0 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -480,20 +480,14 @@ def compute_dict_like( cols = df[key] if cols.ndim == 1: - series_list = [obj._gotitem(key, ndim=1, subset=cols)] + series = obj._gotitem(key, ndim=1, subset=cols) + results.append(getattr(series, op_name)(how, **kwargs)) + keys.append(key) else: - series_list = [] - for index in range(cols.shape[1]): - col = cols.iloc[:, index] - + for _, col in cols.items(): series = obj._gotitem(key, ndim=1, subset=col) - series_list.append(series) - - for series in series_list: - result = getattr(series, op_name)(how, **kwargs) - results.append(result) - keys.append(key) - + results.append(getattr(series, op_name)(how, **kwargs)) + keys.append(key) else: results = [ getattr(obj._gotitem(key, ndim=1), op_name)(how, **kwargs) From de19febf839a73a3cc1e966043332f193f2aa9b4 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:15:30 -0700 Subject: [PATCH 3/4] Undo managers --- pandas/core/apply.py | 1 + pandas/core/internals/managers.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 5959156d11123..4f466b9f56e06 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -482,6 +482,7 @@ def compute_dict_like( df = selected_obj results, keys = [], [] + for key, how in func.items(): cols = df[key] diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index e4b8a2877d5ab..c42ea44b2fc89 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -416,10 +416,11 @@ def apply( result_blocks: list[Block] = [] # fillna: Series/DataFrame is responsible for making sure value is aligned + aligned_args = {k: kwargs[k] for k in align_keys} + for b in self.blocks: - if align_keys: - for k in align_keys: - obj = kwargs[k] + if aligned_args: + for k, obj in aligned_args.items(): if isinstance(obj, (ABCSeries, ABCDataFrame)): # The caller is responsible for ensuring that # obj.axes[-1].equals(self.items) From 02bc89066f922cfd63a633344ab5cded876f5c10 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 22 Jul 2024 13:16:00 -0700 Subject: [PATCH 4/4] Undo whitespace --- pandas/core/apply.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 4f466b9f56e06..5959156d11123 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -482,7 +482,6 @@ def compute_dict_like( df = selected_obj results, keys = [], [] - for key, how in func.items(): cols = df[key]