Skip to content

STYLE: Add code check to avoid returning Exceptions instead of raising #48948

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 9 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
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
Prev Previous commit
Next Next commit
Refactor function to raise
  • Loading branch information
mroeschke committed Oct 4, 2022
commit fe5a0acec34c16728f279383cc043cce0250c5d0
8 changes: 4 additions & 4 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4386,14 +4386,14 @@ def is_int(v):
return indexer

@final
def _invalid_indexer(
def _raise_invalid_indexer(
self,
form: str_t,
key,
reraise: lib.no_default | None | Exception = lib.no_default,
) -> None:
"""
Consistent invalid indexer message.
Raise consistent invalid indexer message.
"""
msg = (
f"cannot do {form} indexing on {type(self).__name__} with these "
Expand Down Expand Up @@ -6691,7 +6691,7 @@ def _validate_indexer(self, form: str_t, key, kind: str_t):
assert kind in ["getitem", "iloc"]

if key is not None and not is_integer(key):
raise self._invalid_indexer(form, key)
self._raise_invalid_indexer(form, key)

def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
"""
Expand Down Expand Up @@ -6723,7 +6723,7 @@ def _maybe_cast_slice_bound(self, label, side: str_t, kind=no_default):
# datetimelike Indexes
# reject them, if index does not contain label
if (is_float(label) or is_integer(label)) and label not in self:
raise self._invalid_indexer("slice", label)
self._raise_invalid_indexer("slice", label)

return label

Expand Down
4 changes: 2 additions & 2 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,12 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
# DTI -> parsing.DateParseError
# TDI -> 'unit abbreviation w/o a number'
# PI -> string cannot be parsed as datetime-like
raise self._invalid_indexer("slice", label) from err
self._raise_invalid_indexer("slice", label, err)

lower, upper = self._parsed_string_to_bounds(reso, parsed)
return lower if side == "left" else upper
elif not isinstance(label, self._data._recognized_scalars):
raise self._invalid_indexer("slice", label)
self._raise_invalid_indexer("slice", label)

return label

Expand Down