Skip to content

Commit 6d08740

Browse files
committed
PERF: Fixed perf regression in TimedeltaIndex.get_loc
Closes pandas-dev#34510
1 parent ba94748 commit 6d08740

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ Deprecations
760760
Performance improvements
761761
~~~~~~~~~~~~~~~~~~~~~~~~
762762

763+
- Fixed performance regression in :meth:`TimedeltaIndex.get_loc` (:issue:`34510`)
763764
- Performance improvement in :class:`Timedelta` constructor (:issue:`30543`)
764765
- Performance improvement in :class:`Timestamp` constructor (:issue:`30543`)
765766
- Performance improvement in flex arithmetic ops between :class:`DataFrame` and :class:`Series` with ``axis=0`` (:issue:`31296`)

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
@@ -212,9 +212,8 @@ def get_loc(self, key, method=None, tolerance=None):
212212
if not is_scalar(key):
213213
raise InvalidIndexError(key)
214214

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

0 commit comments

Comments
 (0)