diff --git a/pandas/_libs/groupby.pyx b/pandas/_libs/groupby.pyx index 3069bbbf34bb7..3f741f08d1363 100644 --- a/pandas/_libs/groupby.pyx +++ b/pandas/_libs/groupby.pyx @@ -441,7 +441,7 @@ def _group_add(floating[:, :] out, floating[:, :] sumx, nobs if len(values) != len(labels): - raise AssertionError("len(index) != len(labels)") + raise ValueError("len(index) != len(labels)") nobs = np.zeros_like(out) sumx = np.zeros_like(out) @@ -491,7 +491,7 @@ def _group_prod(floating[:, :] out, floating[:, :] prodx, nobs if not len(values) == len(labels): - raise AssertionError("len(index) != len(labels)") + raise ValueError("len(index) != len(labels)") nobs = np.zeros_like(out) prodx = np.ones_like(out) @@ -541,7 +541,7 @@ def _group_var(floating[:, :] out, assert min_count == -1, "'min_count' only used in add and prod" if not len(values) == len(labels): - raise AssertionError("len(index) != len(labels)") + raise ValueError("len(index) != len(labels)") nobs = np.zeros_like(out) mean = np.zeros_like(out) @@ -596,7 +596,7 @@ def _group_mean(floating[:, :] out, assert min_count == -1, "'min_count' only used in add and prod" if not len(values) == len(labels): - raise AssertionError("len(index) != len(labels)") + raise ValueError("len(index) != len(labels)") nobs = np.zeros_like(out) sumx = np.zeros_like(out) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index aa817ec451aa5..8cd727e744519 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -261,6 +261,8 @@ def aggregate(self, func=None, *args, **kwargs): try: return self._python_agg_general(func, *args, **kwargs) + except AssertionError: + raise except Exception: result = self._aggregate_named(func, *args, **kwargs) @@ -887,6 +889,8 @@ def aggregate(self, func=None, *args, **kwargs): result = self._aggregate_multiple_funcs( [func], _level=_level, _axis=self.axis ) + except AssertionError: + raise except Exception: result = self._aggregate_frame(func) else: @@ -1036,6 +1040,8 @@ def _aggregate_frame(self, func, *args, **kwargs): for name, data in self: fres = func(data, *args, **kwargs) result[name] = self._try_cast(fres, data) + except AssertionError: + raise except Exception: return self._aggregate_item_by_item(func, *args, **kwargs) else: @@ -1043,6 +1049,8 @@ def _aggregate_frame(self, func, *args, **kwargs): data = self.get_group(name, obj=obj) try: fres = func(data, *args, **kwargs) + except AssertionError: + raise except Exception: wrapper = lambda x: func(x, *args, **kwargs) result[name] = data.apply(wrapper, axis=axis) @@ -1398,6 +1406,8 @@ def _choose_path(self, fast_path, slow_path, group): # if we make it here, test if we can use the fast path try: res_fast = fast_path(group) + except AssertionError: + raise except Exception: # Hard to know ex-ante what exceptions `fast_path` might raise return path, res @@ -1422,6 +1432,8 @@ def _transform_item_by_item(self, obj, wrapper): for i, col in enumerate(obj): try: output[col] = self[col].transform(wrapper) + except AssertionError: + raise except Exception: pass else: diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 92ea733cc3447..6f2868482b798 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -44,13 +44,7 @@ class providing the base-class of operations. from pandas.core import nanops import pandas.core.algorithms as algorithms from pandas.core.arrays import Categorical -from pandas.core.base import ( - DataError, - GroupByError, - PandasObject, - SelectionMixin, - SpecificationError, -) +from pandas.core.base import DataError, PandasObject, SelectionMixin import pandas.core.common as com from pandas.core.construction import extract_array from pandas.core.frame import DataFrame @@ -862,8 +856,6 @@ def _cython_transform(self, how, numeric_only=True, **kwargs): result, names = self.grouper.transform(obj.values, how, **kwargs) except NotImplementedError: continue - except AssertionError as e: - raise GroupByError(str(e)) if self._transform_should_cast(how): output[name] = self._try_cast(result, obj) else: @@ -890,12 +882,7 @@ def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1): if numeric_only and not is_numeric: continue - try: - result, names = self.grouper.aggregate( - obj.values, how, min_count=min_count - ) - except AssertionError as e: - raise GroupByError(str(e)) + result, names = self.grouper.aggregate(obj.values, how, min_count=min_count) output[name] = self._try_cast(result, obj) if len(output) == 0: @@ -1353,8 +1340,8 @@ def f(self, **kwargs): # try a cython aggregation if we can try: return self._cython_agg_general(alias, alt=npfunc, **kwargs) - except AssertionError as e: - raise SpecificationError(str(e)) + except AssertionError: + raise except DataError: pass except Exception: diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 40517eefe4d5d..27415a1bacdbd 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -647,6 +647,8 @@ def _transform( def agg_series(self, obj, func): try: return self._aggregate_series_fast(obj, func) + except AssertionError: + raise except Exception: return self._aggregate_series_pure_python(obj, func) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 545bc21dd6d1b..5185d95cfac4c 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -360,6 +360,8 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs): result = grouped._aggregate_item_by_item(how, *args, **kwargs) else: result = grouped.aggregate(how, *args, **kwargs) + except AssertionError: + raise except Exception: # we have a non-reducing function diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index 7e3cbed09c6d7..5dad868c8c3aa 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -19,7 +19,7 @@ date_range, period_range, ) -from pandas.core.groupby.groupby import SpecificationError +from pandas.core.base import SpecificationError import pandas.util.testing as tm from pandas.io.formats.printing import pprint_thing