Skip to content

PERF: Fixed perf regression in TimedeltaIndex.get_loc #34734

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
Jun 15, 2020
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
10 changes: 8 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,15 +776,19 @@ def _validate_shift_value(self, fill_value):

return self._unbox(fill_value)

def _validate_scalar(self, value, msg: str, cast_str: bool = False):
def _validate_scalar(
self, value, msg: Optional[str] = None, cast_str: bool = False
):
"""
Validate that the input value can be cast to our scalar_type.

Parameters
----------
value : object
msg : str
msg : str, optional.
Message to raise in TypeError on invalid input.
If not provided, `value` is cast to a str and used
as the message.
cast_str : bool, default False
Whether to try to parse string input to scalar_type.

Expand All @@ -807,6 +811,8 @@ def _validate_scalar(self, value, msg: str, cast_str: bool = False):
value = self._scalar_type(value) # type: ignore

else:
if msg is None:
msg = str(value)
raise TypeError(msg)

return value
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@ def get_loc(self, key, method=None, tolerance=None):
if not is_scalar(key):
raise InvalidIndexError(key)

msg = str(key)
try:
key = self._data._validate_scalar(key, msg, cast_str=True)
key = self._data._validate_scalar(key, cast_str=True)
except TypeError as err:
raise KeyError(key) from err

Expand Down