Skip to content

Commit

Permalink
REF: re-raise AssertionError unchanged (pandas-dev#28959)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel authored and proost committed Dec 19, 2019
1 parent 9e5f1b5 commit efdf365
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
8 changes: 4 additions & 4 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,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)
Expand Down Expand Up @@ -492,7 +492,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)
Expand Down Expand Up @@ -542,7 +542,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)
Expand Down Expand Up @@ -597,7 +597,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)
Expand Down
12 changes: 12 additions & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1036,13 +1040,17 @@ 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:
for name in self.indices:
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)
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down
21 changes: 4 additions & 17 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/aggregate/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit efdf365

Please sign in to comment.