From 6682309fbaffc456fd04ebead0b8a83df226187b Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 26 Nov 2021 08:41:07 +0100 Subject: [PATCH 1/3] PERF/TYP: typing cast in __getitem__ gives quite some overhead --- pandas/core/arrays/datetimelike.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 33da9ca858a4c..28eaaa0addf9c 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -332,9 +332,10 @@ def __getitem__( only handle list-likes, slices, and integer scalars """ # Use cast as we know we will get back a DatetimeLikeArray or DTScalar - result = cast( - Union[DatetimeLikeArrayT, DTScalarOrNaT], super().__getitem__(key) - ) + # result = cast( + # Union[DatetimeLikeArrayT, DTScalarOrNaT], super().__getitem__(key) + # ) + result = super().__getitem__(key) if lib.is_scalar(result): return result else: From 9c1a11001d0a847f8db8097a4181d911e56da6f4 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 3 Dec 2021 15:08:16 +0100 Subject: [PATCH 2/3] stringify --- pandas/core/arrays/datetimelike.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 28eaaa0addf9c..bb208785f46fa 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -331,10 +331,12 @@ def __getitem__( This getitem defers to the underlying array, which by-definition can only handle list-likes, slices, and integer scalars """ - # Use cast as we know we will get back a DatetimeLikeArray or DTScalar - # result = cast( - # Union[DatetimeLikeArrayT, DTScalarOrNaT], super().__getitem__(key) - # ) + # Use cast as we know we will get back a DatetimeLikeArray or DTScalar, + # but skip evaluating the Union at runtime for performance + # (see https://github.com/pandas-dev/pandas/pull/44624) + result = cast( + "Union[DatetimeLikeArrayT, DTScalarOrNaT]", super().__getitem__(key) + ) result = super().__getitem__(key) if lib.is_scalar(result): return result From be77452bb3a41c0c8be116e87f7aa3fe4fb14eae Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 3 Dec 2021 15:53:33 +0100 Subject: [PATCH 3/3] categorical as well --- pandas/core/arrays/categorical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 16fcf6daf5aef..9cfcd0adc92ec 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -509,7 +509,7 @@ def astype(self, dtype: AstypeArg, copy: bool = True) -> ArrayLike: result = self.copy() if copy else self elif is_categorical_dtype(dtype): - dtype = cast(Union[str, CategoricalDtype], dtype) + dtype = cast("Union[str, CategoricalDtype]", dtype) # GH 10696/18593/18630 dtype = self.dtype.update_dtype(dtype)