Skip to content

Commit ed1f7aa

Browse files
PERF: Fixed perf regression in TimedeltaIndex.get_loc (#34734)
1 parent 60915b4 commit ed1f7aa

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pandas/core/arrays/datetimelike.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -776,15 +776,19 @@ def _validate_shift_value(self, fill_value):
776776

777777
return self._unbox(fill_value)
778778

779-
def _validate_scalar(self, value, msg: str, cast_str: bool = False):
779+
def _validate_scalar(
780+
self, value, msg: Optional[str] = None, cast_str: bool = False
781+
):
780782
"""
781783
Validate that the input value can be cast to our scalar_type.
782784
783785
Parameters
784786
----------
785787
value : object
786-
msg : str
788+
msg : str, optional.
787789
Message to raise in TypeError on invalid input.
790+
If not provided, `value` is cast to a str and used
791+
as the message.
788792
cast_str : bool, default False
789793
Whether to try to parse string input to scalar_type.
790794
@@ -807,6 +811,8 @@ def _validate_scalar(self, value, msg: str, cast_str: bool = False):
807811
value = self._scalar_type(value) # type: ignore
808812

809813
else:
814+
if msg is None:
815+
msg = str(value)
810816
raise TypeError(msg)
811817

812818
return value

pandas/core/indexes/timedeltas.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@ def get_loc(self, key, method=None, tolerance=None):
213213
if not is_scalar(key):
214214
raise InvalidIndexError(key)
215215

216-
msg = str(key)
217216
try:
218-
key = self._data._validate_scalar(key, msg, cast_str=True)
217+
key = self._data._validate_scalar(key, cast_str=True)
219218
except TypeError as err:
220219
raise KeyError(key) from err
221220

0 commit comments

Comments
 (0)