From 9f29ac7bd2a13825b2891cf7c1b7e79c691c2fed Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 13 Oct 2019 15:46:55 -0700 Subject: [PATCH 1/2] REF: re-reraise AssertionError unchanged --- pandas/_libs/groupby.pyx | 8 ++++---- pandas/core/groupby/generic.py | 12 ++++++++++++ pandas/core/groupby/groupby.py | 21 ++++----------------- pandas/core/groupby/ops.py | 2 ++ pandas/core/resample.py | 2 ++ 5 files changed, 24 insertions(+), 21 deletions(-) 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 068d5e5275f0d..d69c05999fcf2 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -251,6 +251,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) @@ -877,6 +879,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: @@ -1026,6 +1030,8 @@ def _aggregate_frame(self, func, *args, **kwargs): try: for name, data in self: result[name] = self._try_cast(func(data, *args, **kwargs), data) + except AssertionError: + raise except Exception: return self._aggregate_item_by_item(func, *args, **kwargs) else: @@ -1033,6 +1039,8 @@ def _aggregate_frame(self, func, *args, **kwargs): try: data = self.get_group(name, obj=obj) result[name] = self._try_cast(func(data, *args, **kwargs), data) + except AssertionError: + raise except Exception: wrapper = lambda x: func(x, *args, **kwargs) result[name] = data.apply(wrapper, axis=axis) @@ -1386,6 +1394,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 @@ -1411,6 +1421,8 @@ def _transform_item_by_item(self, obj, wrapper): try: output[col] = self[col].transform(wrapper) inds.append(i) + except AssertionError: + raise except Exception: pass diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cc297629a7004..e273562c30739 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 @@ -869,8 +863,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: @@ -897,12 +889,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: @@ -1359,8 +1346,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 Exception: pass 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 From 851f6e1f3fe0ab54cce522b7ff7bc293d2bac267 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sun, 13 Oct 2019 16:57:17 -0700 Subject: [PATCH 2/2] update import --- pandas/tests/groupby/aggregate/test_other.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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