Skip to content

PERF: Improve performance for astype is view #57478

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 7 commits into from
Mar 18, 2024
Merged
Changes from 4 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
5 changes: 5 additions & 0 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool:
-------
True if new data is a view or not guaranteed to be a copy, False otherwise
"""
if dtype.kind in "iufb" and dtype.kind == new_dtype.kind:
# fastpath for numeric dtypes
if hasattr(dtype, "itemsize") and hasattr(new_dtype, "itemsize"):
return dtype.itemsize == new_dtype.itemsize # type: ignore[reportGeneralTypeIssues]

if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype):
new_dtype, dtype = dtype, new_dtype

Expand Down