Skip to content

CLN: catch specific exceptions in frame.py #29258

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 3 commits into from
Oct 29, 2019
Merged
Changes from 1 commit
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
20 changes: 11 additions & 9 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1664,11 +1664,12 @@ def from_records(
else:
try:
index_data = [arrays[arr_columns.get_loc(field)] for field in index]
except KeyError:
# raised by get_loc
Copy link
Member

Choose a reason for hiding this comment

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

Unfortunately, there are cases that get_loc returns a different error .. (if you look in the get_loc code, there are eg a few places where we deliberately raise a TypeError. Separate issue, but we should maybe consider changing that)
I don't know if this in the end will have any impact on the result here (as those cases probably mean you do not have correct input data anyway), but probably something to check to be sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately, there are cases that get_loc returns a different error

Darn, thanks for bringing this to my attention. I'll update this to also catch TypeError.

Separate issue, but we should maybe consider changing that

Good idea, will take a look.

result_index = index
else:
result_index = ensure_index_from_sequences(index_data, names=index)

exclude.update(index)
except Exception:
result_index = index

if any(exclude):
arr_exclude = [x for x in exclude if x in arr_columns]
Expand Down Expand Up @@ -3623,11 +3624,11 @@ def reindexer(value):
# GH 4107
try:
value = value.reindex(self.index)._values
except Exception as e:

# duplicate axis
except ValueError as err:
# raised in MultiIndex.from_tuples, see test_insert_error_msmgs
if not value.index.is_unique:
raise e
# duplicate axis
raise err

# other
raise TypeError(
Expand Down Expand Up @@ -7794,7 +7795,8 @@ def f(x):
# TODO: combine with hasattr(result, 'dtype') further down
# hard since we don't have `values` down there.
result = np.bool_(result)
except Exception as e:
except TypeError as err:
# e.g. in nanops trying to convert strs to float

# try by-column first
if filter_type is None and axis == 0:
Expand Down Expand Up @@ -7824,7 +7826,7 @@ def f(x):
raise NotImplementedError(
"Handling exception with filter_type {f} not"
"implemented.".format(f=filter_type)
) from e
) from err
with np.errstate(all="ignore"):
result = f(data.values)
labels = data._get_agg_axis(axis)
Expand Down