From 0a887b04e9112d3fd6b23d9424b37b1b1eb1fde3 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 14 Sep 2021 19:14:34 -0700 Subject: [PATCH] PERF: grouped_reduce --- pandas/core/internals/managers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 386a4ef12e6b5..cadd5026daff2 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1248,6 +1248,7 @@ def grouped_reduce(self: T, func: Callable, ignore_failures: bool = False) -> T: BlockManager """ result_blocks: list[Block] = [] + dropped_any = False for blk in self.blocks: if blk.is_object: @@ -1259,6 +1260,7 @@ def grouped_reduce(self: T, func: Callable, ignore_failures: bool = False) -> T: except (TypeError, NotImplementedError): if not ignore_failures: raise + dropped_any = True continue result_blocks = extend_blocks(applied, result_blocks) else: @@ -1267,6 +1269,7 @@ def grouped_reduce(self: T, func: Callable, ignore_failures: bool = False) -> T: except (TypeError, NotImplementedError): if not ignore_failures: raise + dropped_any = True continue result_blocks = extend_blocks(applied, result_blocks) @@ -1275,7 +1278,8 @@ def grouped_reduce(self: T, func: Callable, ignore_failures: bool = False) -> T: else: index = Index(range(result_blocks[0].values.shape[-1])) - if ignore_failures: + if dropped_any: + # faster to skip _combine if we haven't dropped any blocks return self._combine(result_blocks, copy=False, index=index) return type(self).from_blocks(result_blocks, [self.axes[0], index])