From bd3cd98ebf3a425165e84c4609978b40bc11693f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 12 Jun 2020 11:56:59 -0500 Subject: [PATCH 1/2] PERF: Fixed perf regression in TimedeltaIndex.get_loc Closes https://github.com/pandas-dev/pandas/issues/34510 --- doc/source/whatsnew/v1.1.0.rst | 1 + pandas/core/arrays/datetimelike.py | 10 ++++++++-- pandas/core/indexes/timedeltas.py | 3 +-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 92f7c0f6b59a3..c4eb9b5e06f34 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -760,6 +760,7 @@ Deprecations Performance improvements ~~~~~~~~~~~~~~~~~~~~~~~~ +- Fixed performance regression in :meth:`TimedeltaIndex.get_loc` (:issue:`34510`) - Performance improvement in :class:`Timedelta` constructor (:issue:`30543`) - Performance improvement in :class:`Timestamp` constructor (:issue:`30543`) - Performance improvement in flex arithmetic ops between :class:`DataFrame` and :class:`Series` with ``axis=0`` (:issue:`31296`) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 8af23815b54ef..1fea6ca1b8a3d 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -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. @@ -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 diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index ce3ff17814a25..c5ad0bf0d5ddf 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -212,9 +212,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 From fd9599d22bbd930dee5ca83a9ce47f18dcf6724f Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 15 Jun 2020 07:34:41 -0500 Subject: [PATCH 2/2] no whatsnew --- doc/source/whatsnew/v1.1.0.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 9beb23638641a..f7e36de059e84 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -781,7 +781,6 @@ Deprecations Performance improvements ~~~~~~~~~~~~~~~~~~~~~~~~ -- Fixed performance regression in :meth:`TimedeltaIndex.get_loc` (:issue:`34510`) - Performance improvement in :class:`Timedelta` constructor (:issue:`30543`) - Performance improvement in :class:`Timestamp` constructor (:issue:`30543`) - Performance improvement in flex arithmetic ops between :class:`DataFrame` and :class:`Series` with ``axis=0`` (:issue:`31296`)