Skip to content

CLN: tighten exception catching in indexes #29078

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 4 commits into from
Oct 19, 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
17 changes: 4 additions & 13 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3142,16 +3142,7 @@ def is_int(v):
elif is_positional:
indexer = key
else:
try:
indexer = self.slice_indexer(start, stop, step, kind=kind)
except Exception:
if is_index_slice:
if self.is_integer():
raise
else:
indexer = key
else:
raise
indexer = self.slice_indexer(start, stop, step, kind=kind)

return indexer

Expand Down Expand Up @@ -4676,11 +4667,11 @@ def get_value(self, series, key):
raise InvalidIndexError(key)
else:
raise e1
except Exception: # pragma: no cover
except Exception:
raise e1
except TypeError:
# python 3
if is_scalar(key): # pragma: no cover
# e.g. "[False] is an invalid key"
if is_scalar(key):
raise IndexError(key)
raise InvalidIndexError(key)

Expand Down
10 changes: 8 additions & 2 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,11 @@ def __contains__(self, key):
try:
self.get_loc(key)
return True
except Exception:
except (ValueError, TypeError, KeyError):
# TypeError can be reached if we pass a tuple that is not hashable
# ValueError can be reached if pass a 2-tuple and parse_time_string
# raises with the wrong number of return values
# TODO: the latter is a bug in parse_time_string
return False

@cache_readonly
Expand Down Expand Up @@ -765,7 +769,9 @@ def _maybe_cast_slice_bound(self, label, side, kind):
_, parsed, reso = parse_time_string(label, self.freq)
bounds = self._parsed_string_to_bounds(reso, parsed)
return bounds[0 if side == "left" else 1]
except Exception:
except ValueError:
# string cannot be parsed as datetime-like
# TODO: we need tests for this case
raise KeyError(label)
elif is_integer(label) or is_float(label):
self._invalid_indexer("slice", label)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,8 @@ def insert(self, loc, item):
if _is_convertible_to_td(item):
try:
item = Timedelta(item)
except Exception:
except ValueError:
# e.g. str that can't be parsed to timedelta
pass
elif is_scalar(item) and isna(item):
# GH 18295
Expand Down