Skip to content

CLN: unnecessary exception catching #29298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 3 additions & 16 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
)
from pandas.core.dtypes.generic import ABCSeries

from pandas.io.formats.printing import pprint_thing


def frame_apply(
obj,
Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think this can be informative, though.
Is this that problematic? (we are raising the same exception again, so it is not hiding any exceptions?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The index information can be useful, but this can also make it harder to reason about what parts of the code are exception-raising.

This can go either way. If there's a consensus to keep it, I'll revert.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how this makes it harder to reason about? This simply reraises what has been raised (with an edited error message), no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain how this makes it harder to reason about?

Well it's an idiosyncratic thing, so no.


self.results = results
self.res_index = res_index
Expand Down
4 changes: 0 additions & 4 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down