From 66cb166c96173ac53516f2e4292edd4a4c40ece7 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 7 Nov 2019 12:48:24 -0800 Subject: [PATCH] CLN: unnecessary exception catching (#29298) --- pandas/_libs/reduction.pyx | 4 ---- pandas/core/apply.py | 19 +++---------------- pandas/tests/frame/test_apply.py | 7 ++----- 3 files changed, 5 insertions(+), 25 deletions(-) diff --git a/pandas/_libs/reduction.pyx b/pandas/_libs/reduction.pyx index 45991293ba5bc..60c65a22603cd 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/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index a1172610b847e..fea50b3b7f75d 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -423,12 +423,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):