From 01cbc38bc8e6591899d9f1756fcdce87c726837e Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 2 May 2021 08:39:33 -0700 Subject: [PATCH] REF: avoid unnecessary raise in DataFrameGroupBy._cython_agg_general --- pandas/core/apply.py | 5 ++++- pandas/core/groupby/generic.py | 4 +++- pandas/core/groupby/groupby.py | 6 +++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 1aa08356982d2..693b1832ed3c9 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -360,7 +360,10 @@ def agg_list_like(self) -> FrameOrSeriesUnion: # raised directly in _aggregate_named pass elif "no results" in str(err): - # raised directly in _aggregate_multiple_funcs + # reached in test_frame_apply.test_nuiscance_columns + # where the colg.aggregate(arg) ends up going through + # the selected_obj.ndim == 1 branch above with arg == ["sum"] + # on a datetime64[ns] column pass else: raise diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index b9f1ca0710872..953ff8b640a8d 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1100,6 +1100,7 @@ def _cython_agg_general( # Note: we never get here with how="ohlc"; that goes through SeriesGroupBy data: Manager2D = self._get_data_to_aggregate() + orig = data if numeric_only: data = data.get_numeric_data(copy=False) @@ -1177,7 +1178,8 @@ def array_func(values: ArrayLike) -> ArrayLike: # continue and exclude the block new_mgr = data.grouped_reduce(array_func, ignore_failures=True) - if not len(new_mgr): + if not len(new_mgr) and len(orig): + # If the original Manager was already empty, no need to raise raise DataError("No numeric types to aggregate") return self._wrap_agged_manager(new_mgr) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 7fe9d7cb49eb5..ec44ff9cf0dce 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -1287,7 +1287,11 @@ def _agg_general( ) or "category dtype not supported" in str(err): # raised in _get_cython_function, in some cases can # be trimmed by implementing cython funcs for more dtypes - pass + if self.obj.ndim != 1: + # We expect to get here for SeriesGroupBy; but for + # DataFrameGroupBy these should be handled within + # _cython_agg_general + raise else: raise