Skip to content

Commit bceac8e

Browse files
jbrockmendeljreback
authored andcommitted
CLN: no longer need to catch AttributeError, IndexError (#29591)
1 parent 2deb2da commit bceac8e

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

pandas/_libs/reduction.pyx

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ cdef class SeriesBinGrouper(_BaseGrouper):
221221
def __init__(self, object series, object f, object bins, object dummy):
222222

223223
assert dummy is not None # always obj[:0]
224+
assert len(bins) > 0 # otherwise we get IndexError in get_result
224225

225226
self.bins = bins
226227
self.f = f

pandas/core/groupby/generic.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,7 @@ def aggregate(self, func=None, *args, **kwargs):
257257

258258
try:
259259
return self._python_agg_general(func, *args, **kwargs)
260-
except (ValueError, KeyError, AttributeError, IndexError):
261-
# TODO: IndexError can be removed here following GH#29106
262-
# TODO: AttributeError is caused by _index_data hijinx in
263-
# libreduction, can be removed after GH#29160
260+
except (ValueError, KeyError):
264261
# TODO: KeyError is raised in _python_agg_general,
265262
# see see test_groupby.test_basic
266263
result = self._aggregate_named(func, *args, **kwargs)

pandas/core/groupby/ops.py

+5
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,10 @@ def __init__(
721721
self.mutated = mutated
722722
self.indexer = indexer
723723

724+
# These lengths must match, otherwise we could call agg_series
725+
# with empty self.bins, which would raise in libreduction.
726+
assert len(self.binlabels) == len(self.bins)
727+
724728
@cache_readonly
725729
def groups(self):
726730
""" dict {group name -> group labels} """
@@ -828,6 +832,7 @@ def groupings(self) -> "List[grouper.Grouping]":
828832
def agg_series(self, obj: Series, func):
829833
# Caller is responsible for checking ngroups != 0
830834
assert self.ngroups != 0
835+
assert len(self.bins) > 0 # otherwise we'd get IndexError in get_result
831836

832837
if is_extension_array_dtype(obj.dtype):
833838
# pre-empt SeriesBinGrouper from raising TypeError

pandas/core/resample.py

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ def _get_binner(self):
187187
"""
188188

189189
binner, bins, binlabels = self._get_binner_for_time()
190+
assert len(bins) == len(binlabels)
190191
bin_grouper = BinGrouper(bins, binlabels, indexer=self.groupby.indexer)
191192
return binner, bin_grouper
192193

0 commit comments

Comments
 (0)