Skip to content

PERF/TYP: typing cast in __getitem__ gives quite some overhead #44624

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
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
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,13 @@ 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
# 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)
"Union[DatetimeLikeArrayT, DTScalarOrNaT]", super().__getitem__(key)
)
result = super().__getitem__(key)
if lib.is_scalar(result):
return result
else:
Expand Down