From 4d918c95b34df2f8c471fa187758f7aa51a61027 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 30 Oct 2019 15:52:11 -0700 Subject: [PATCH] CLN: unnecessary exception catching --- pandas/_libs/reduction.pyx | 4 ---- pandas/core/apply.py | 19 +++---------------- pandas/core/groupby/generic.py | 4 ---- pandas/core/resample.py | 2 -- pandas/tests/frame/test_apply.py | 7 ++----- 5 files changed, 5 insertions(+), 31 deletions(-) diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index f505c0479e944..287a7bb0a7dea 100644 --- a/pandas/_libs/reduction.pyx +++ b/pandas/_libs/reduction.pyx @@ -170,10 +170,6 @@ cdef class Reducer: PyArray_SETITEM(result, PyArray_ITER_DATA(it), res) chunk.data = chunk.data + self.increment PyArray_ITER_NEXT(it) - except Exception as err: - if hasattr(err, 'args'): - err.args = err.args + (i,) - raise finally: # so we don't free the wrong memory chunk.data = dummy_buf diff --git a/pandas/core/apply.py b/pandas/core/apply.py index f402154dc91ca..d0093e5b697e1 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -13,8 +13,6 @@ ) from pandas.core.dtypes.generic import ABCSeries -from pandas.io.formats.printing import pprint_thing - def frame_apply( obj, @@ -293,20 +291,9 @@ def apply_series_generator(self): res_index = res_index.take(successes) else: - try: - for i, v in enumerate(series_gen): - results[i] = self.f(v) - keys.append(v.name) - except Exception as err: - if hasattr(err, "args"): - - # make sure i is defined - if i is not None: - k = res_index[i] - err.args = err.args + ( - "occurred at index %s" % pprint_thing(k), - ) - raise + for i, v in enumerate(series_gen): + results[i] = self.f(v) + keys.append(v.name) self.results = results self.res_index = res_index diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index f4c3ac970a3ca..606fd4ed4c360 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -262,8 +262,6 @@ def aggregate(self, func=None, *args, **kwargs): try: return self._python_agg_general(func, *args, **kwargs) - except (AssertionError, TypeError): - raise except (ValueError, KeyError, AttributeError, IndexError): # TODO: IndexError can be removed here following GH#29106 # TODO: AttributeError is caused by _index_data hijinx in @@ -1465,8 +1463,6 @@ 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 TypeError: # e.g. trying to call nanmean with string values pass diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 13cb0f9aed303..0e36726ac5f97 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -361,8 +361,6 @@ 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 DataError: # we have a non-reducing function; try to evaluate result = grouped.apply(how, *args, **kwargs) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 4b7439cd40023..b6330178a23f9 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -424,12 +424,9 @@ def transform2(row): row["D"] = 7 return row - try: + msg = "'float' object has no attribute 'startswith'" + with pytest.raises(AttributeError, match=msg): data.apply(transform, axis=1) - except AttributeError as e: - assert len(e.args) == 2 - assert e.args[1] == "occurred at index 4" - assert e.args[0] == "'float' object has no attribute 'startswith'" def test_apply_bug(self):