From ee5d945e3ff23534f3b88f7bd8499edfbfe9039f Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 18 Feb 2024 00:15:38 +0100 Subject: [PATCH 1/6] PERF: Improve performance for astype is view --- pandas/core/dtypes/astype.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index f5579082c679b..12dfd1cdd64a2 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -258,6 +258,10 @@ 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 + return dtype.itemsize == new_dtype.itemsize + if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype): new_dtype, dtype = dtype, new_dtype From fcd9536c7b76713c57dc83b4eb58232425e7229a Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 18 Feb 2024 01:21:06 +0100 Subject: [PATCH 2/6] Update --- pandas/core/dtypes/astype.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 12dfd1cdd64a2..74b0269857d02 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -260,7 +260,8 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool: """ if dtype.kind in "iufb" and dtype.kind == new_dtype.kind: # fastpath for numeric dtypes - return dtype.itemsize == new_dtype.itemsize + if hasattr(dtype, "itemsize") and hasattr(new_dtype, "itemsize"): + return dtype.itemsize == new_dtype.itemsize if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype): new_dtype, dtype = dtype, new_dtype From eee671c369171dbc4977f01f400ebca4f5c2623f Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Sun, 18 Feb 2024 13:39:31 +0100 Subject: [PATCH 3/6] Update --- pandas/core/dtypes/astype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 74b0269857d02..16aa008e96493 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -260,7 +260,7 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool: """ if dtype.kind in "iufb" and dtype.kind == new_dtype.kind: # fastpath for numeric dtypes - if hasattr(dtype, "itemsize") and hasattr(new_dtype, "itemsize"): + if hasattr(dtype, "itemsize") and hasattr(new_dtype, "itemsize"): # type: ignore[reportGeneralTypeIssues] return dtype.itemsize == new_dtype.itemsize if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype): From c009b0f757365b22d5d83a990f161bd5178ab1ff Mon Sep 17 00:00:00 2001 From: Patrick Hoefler Date: Mon, 19 Feb 2024 23:46:58 +0100 Subject: [PATCH 4/6] Fixup --- pandas/core/dtypes/astype.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 16aa008e96493..910a9b18d8a61 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -260,8 +260,8 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool: """ if dtype.kind in "iufb" and dtype.kind == new_dtype.kind: # fastpath for numeric dtypes - if hasattr(dtype, "itemsize") and hasattr(new_dtype, "itemsize"): # type: ignore[reportGeneralTypeIssues] - return dtype.itemsize == new_dtype.itemsize + 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 From d48f71903fa45479c8bf59664d28f214223b7e60 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 16 Mar 2024 12:44:27 -0500 Subject: [PATCH 5/6] Update astype.py --- pandas/core/dtypes/astype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index 910a9b18d8a61..a98acc701d532 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -261,7 +261,7 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool: 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] + return dtype.itemsize == new_dtype.itemsize # pyright: ignore[reportGeneralTypeIssues] if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype): new_dtype, dtype = dtype, new_dtype From 2e7ab0ec5d2c5d6f4e8942033a97888690349cf1 Mon Sep 17 00:00:00 2001 From: Patrick Hoefler <61934744+phofl@users.noreply.github.com> Date: Sat, 16 Mar 2024 18:12:09 -0500 Subject: [PATCH 6/6] Update astype.py --- pandas/core/dtypes/astype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/astype.py b/pandas/core/dtypes/astype.py index a98acc701d532..218d1a70232a1 100644 --- a/pandas/core/dtypes/astype.py +++ b/pandas/core/dtypes/astype.py @@ -261,7 +261,7 @@ def astype_is_view(dtype: DtypeObj, new_dtype: DtypeObj) -> bool: 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 # pyright: ignore[reportGeneralTypeIssues] + return dtype.itemsize == new_dtype.itemsize # pyright: ignore[reportAttributeAccessIssue] if isinstance(dtype, np.dtype) and not isinstance(new_dtype, np.dtype): new_dtype, dtype = dtype, new_dtype