diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 647eb5a2daa28..f4c3ac970a3ca 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -1104,6 +1104,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs): # raised in _aggregate_named, handle at higher level # see test_apply_with_mutated_index raise + # otherwise we get here from an AttributeError in _make_wrapper cannot_agg.append(item) continue @@ -1466,7 +1467,8 @@ def _transform_item_by_item(self, obj, wrapper): output[col] = self[col].transform(wrapper) except AssertionError: raise - except Exception: + except TypeError: + # e.g. trying to call nanmean with string values pass else: inds.append(i) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 7d1c74e415658..e53e7ffdbf72f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -641,12 +641,15 @@ def curried(x): # if we don't have this method to indicated to aggregate to # mark this column as an error try: - return self._aggregate_item_by_item(name, *args, **kwargs) + result = self._aggregate_item_by_item(name, *args, **kwargs) + assert self.obj.ndim == 2 + return result except AttributeError: # e.g. SparseArray has no flags attr # FIXME: 'SeriesGroupBy' has no attribute '_aggregate_item_by_item' # occurs in idxmax() case # in tests.groupby.test_function.test_non_cython_api + assert self.obj.ndim == 1 raise ValueError wrapper.__name__ = name diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 79b51ef57cd37..a34e002e7a144 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -669,13 +669,16 @@ def agg_series(self, obj, func): if is_extension_array_dtype(obj.dtype) and obj.dtype.kind != "M": # _aggregate_series_fast would raise TypeError when # calling libreduction.Slider + # TODO: can we get a performant workaround for EAs backed by ndarray? # TODO: is the datetime64tz case supposed to go through here? return self._aggregate_series_pure_python(obj, func) + elif obj.index._has_complex_internals: + # MultiIndex; Pre-empt TypeError in _aggregate_series_fast + return self._aggregate_series_pure_python(obj, func) + try: return self._aggregate_series_fast(obj, func) - except AssertionError: - raise except ValueError as err: if "No result." in str(err): # raised in libreduction @@ -685,12 +688,6 @@ def agg_series(self, obj, func): pass else: raise - except TypeError as err: - if "ndarray" in str(err): - # raised in libreduction if obj's values is no ndarray - pass - else: - raise return self._aggregate_series_pure_python(obj, func) def _aggregate_series_fast(self, obj, func):