Skip to content

Commit 143eb38

Browse files
jbrockmendelWillAyd
authored andcommitted
REF: re-raise AssertionError unchanged (pandas-dev#28959)
1 parent 30a0e2e commit 143eb38

File tree

6 files changed

+25
-22
lines changed

6 files changed

+25
-22
lines changed

pandas/_libs/groupby.pyx

+4-4
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def _group_add(floating[:, :] out,
442442
floating[:, :] sumx, nobs
443443

444444
if len(values) != len(labels):
445-
raise AssertionError("len(index) != len(labels)")
445+
raise ValueError("len(index) != len(labels)")
446446

447447
nobs = np.zeros_like(out)
448448
sumx = np.zeros_like(out)
@@ -492,7 +492,7 @@ def _group_prod(floating[:, :] out,
492492
floating[:, :] prodx, nobs
493493

494494
if not len(values) == len(labels):
495-
raise AssertionError("len(index) != len(labels)")
495+
raise ValueError("len(index) != len(labels)")
496496

497497
nobs = np.zeros_like(out)
498498
prodx = np.ones_like(out)
@@ -542,7 +542,7 @@ def _group_var(floating[:, :] out,
542542
assert min_count == -1, "'min_count' only used in add and prod"
543543

544544
if not len(values) == len(labels):
545-
raise AssertionError("len(index) != len(labels)")
545+
raise ValueError("len(index) != len(labels)")
546546

547547
nobs = np.zeros_like(out)
548548
mean = np.zeros_like(out)
@@ -597,7 +597,7 @@ def _group_mean(floating[:, :] out,
597597
assert min_count == -1, "'min_count' only used in add and prod"
598598

599599
if not len(values) == len(labels):
600-
raise AssertionError("len(index) != len(labels)")
600+
raise ValueError("len(index) != len(labels)")
601601

602602
nobs = np.zeros_like(out)
603603
sumx = np.zeros_like(out)

pandas/core/groupby/generic.py

+12
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def aggregate(self, func=None, *args, **kwargs):
261261

262262
try:
263263
return self._python_agg_general(func, *args, **kwargs)
264+
except AssertionError:
265+
raise
264266
except Exception:
265267
result = self._aggregate_named(func, *args, **kwargs)
266268

@@ -887,6 +889,8 @@ def aggregate(self, func=None, *args, **kwargs):
887889
result = self._aggregate_multiple_funcs(
888890
[func], _level=_level, _axis=self.axis
889891
)
892+
except AssertionError:
893+
raise
890894
except Exception:
891895
result = self._aggregate_frame(func)
892896
else:
@@ -1036,13 +1040,17 @@ def _aggregate_frame(self, func, *args, **kwargs):
10361040
for name, data in self:
10371041
fres = func(data, *args, **kwargs)
10381042
result[name] = self._try_cast(fres, data)
1043+
except AssertionError:
1044+
raise
10391045
except Exception:
10401046
return self._aggregate_item_by_item(func, *args, **kwargs)
10411047
else:
10421048
for name in self.indices:
10431049
data = self.get_group(name, obj=obj)
10441050
try:
10451051
fres = func(data, *args, **kwargs)
1052+
except AssertionError:
1053+
raise
10461054
except Exception:
10471055
wrapper = lambda x: func(x, *args, **kwargs)
10481056
result[name] = data.apply(wrapper, axis=axis)
@@ -1398,6 +1406,8 @@ def _choose_path(self, fast_path, slow_path, group):
13981406
# if we make it here, test if we can use the fast path
13991407
try:
14001408
res_fast = fast_path(group)
1409+
except AssertionError:
1410+
raise
14011411
except Exception:
14021412
# Hard to know ex-ante what exceptions `fast_path` might raise
14031413
return path, res
@@ -1422,6 +1432,8 @@ def _transform_item_by_item(self, obj, wrapper):
14221432
for i, col in enumerate(obj):
14231433
try:
14241434
output[col] = self[col].transform(wrapper)
1435+
except AssertionError:
1436+
raise
14251437
except Exception:
14261438
pass
14271439
else:

pandas/core/groupby/groupby.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,7 @@ class providing the base-class of operations.
4444
from pandas.core import nanops
4545
import pandas.core.algorithms as algorithms
4646
from pandas.core.arrays import Categorical
47-
from pandas.core.base import (
48-
DataError,
49-
GroupByError,
50-
PandasObject,
51-
SelectionMixin,
52-
SpecificationError,
53-
)
47+
from pandas.core.base import DataError, PandasObject, SelectionMixin
5448
import pandas.core.common as com
5549
from pandas.core.construction import extract_array
5650
from pandas.core.frame import DataFrame
@@ -862,8 +856,6 @@ def _cython_transform(self, how, numeric_only=True, **kwargs):
862856
result, names = self.grouper.transform(obj.values, how, **kwargs)
863857
except NotImplementedError:
864858
continue
865-
except AssertionError as e:
866-
raise GroupByError(str(e))
867859
if self._transform_should_cast(how):
868860
output[name] = self._try_cast(result, obj)
869861
else:
@@ -890,12 +882,7 @@ def _cython_agg_general(self, how, alt=None, numeric_only=True, min_count=-1):
890882
if numeric_only and not is_numeric:
891883
continue
892884

893-
try:
894-
result, names = self.grouper.aggregate(
895-
obj.values, how, min_count=min_count
896-
)
897-
except AssertionError as e:
898-
raise GroupByError(str(e))
885+
result, names = self.grouper.aggregate(obj.values, how, min_count=min_count)
899886
output[name] = self._try_cast(result, obj)
900887

901888
if len(output) == 0:
@@ -1353,8 +1340,8 @@ def f(self, **kwargs):
13531340
# try a cython aggregation if we can
13541341
try:
13551342
return self._cython_agg_general(alias, alt=npfunc, **kwargs)
1356-
except AssertionError as e:
1357-
raise SpecificationError(str(e))
1343+
except AssertionError:
1344+
raise
13581345
except DataError:
13591346
pass
13601347
except Exception:

pandas/core/groupby/ops.py

+2
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ def _transform(
647647
def agg_series(self, obj, func):
648648
try:
649649
return self._aggregate_series_fast(obj, func)
650+
except AssertionError:
651+
raise
650652
except Exception:
651653
return self._aggregate_series_pure_python(obj, func)
652654

pandas/core/resample.py

+2
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ def _groupby_and_aggregate(self, how, grouper=None, *args, **kwargs):
360360
result = grouped._aggregate_item_by_item(how, *args, **kwargs)
361361
else:
362362
result = grouped.aggregate(how, *args, **kwargs)
363+
except AssertionError:
364+
raise
363365
except Exception:
364366

365367
# we have a non-reducing function

pandas/tests/groupby/aggregate/test_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
date_range,
2020
period_range,
2121
)
22-
from pandas.core.groupby.groupby import SpecificationError
22+
from pandas.core.base import SpecificationError
2323
import pandas.util.testing as tm
2424

2525
from pandas.io.formats.printing import pprint_thing

0 commit comments

Comments
 (0)