diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index e514162f84c37..e731cffea0671 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -324,7 +324,11 @@ def _aggregate_item_by_item(self, func, *args, **kwargs): if cast: result[item] = self._try_cast(result[item], data) - except ValueError: + except ValueError as err: + if "Must produce aggregated value" in str(err): + # raised in _aggregate_named, handle at higher level + # see test_apply_with_mutated_index + raise cannot_agg.append(item) continue except TypeError as e: @@ -1009,7 +1013,7 @@ def _aggregate_named(self, func, *args, **kwargs): group.name = name output = func(group, *args, **kwargs) if isinstance(output, (Series, Index, np.ndarray)): - raise Exception("Must produce aggregated value") + raise ValueError("Must produce aggregated value") result[name] = self._try_cast(output, group) return result