From 7841851b0eac5b9b42702fd52561dd4e22f3aacb Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Sun, 7 Sep 2025 11:26:21 +0200 Subject: [PATCH 1/4] TYP: stricter typing of return value of to_numpy_dtype_inference helper function --- pandas/core/arrays/_utils.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/pandas/core/arrays/_utils.py b/pandas/core/arrays/_utils.py index 6b46396d5efdf..4a57183f22a74 100644 --- a/pandas/core/arrays/_utils.py +++ b/pandas/core/arrays/_utils.py @@ -22,26 +22,27 @@ def to_numpy_dtype_inference( arr: ArrayLike, dtype: npt.DTypeLike | None, na_value, hasna: bool -) -> tuple[npt.DTypeLike, Any]: +) -> tuple[np.dtype | None, Any]: + result_dtype: np.dtype | None + inferred_numeric_dtype = False if dtype is None and is_numeric_dtype(arr.dtype): - dtype_given = False + inferred_numeric_dtype = True if hasna: if arr.dtype.kind == "b": - dtype = np.dtype(np.object_) + result_dtype = np.dtype(np.object_) else: if arr.dtype.kind in "iu": - dtype = np.dtype(np.float64) + result_dtype = np.dtype(np.float64) else: - dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] + result_dtype = arr.dtype.numpy_dtype if na_value is lib.no_default: na_value = np.nan else: - dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] + result_dtype = arr.dtype.numpy_dtype elif dtype is not None: - dtype = np.dtype(dtype) - dtype_given = True + result_dtype = np.dtype(dtype) else: - dtype_given = True + result_dtype = None if na_value is lib.no_default: if dtype is None or not hasna: @@ -55,9 +56,9 @@ def to_numpy_dtype_inference( else: na_value = arr.dtype.na_value - if not dtype_given and hasna: + if inferred_numeric_dtype and hasna: try: - np_can_hold_element(dtype, na_value) # type: ignore[arg-type] + np_can_hold_element(result_dtype, na_value) # type: ignore[arg-type] except LossySetitemError: - dtype = np.dtype(np.object_) - return dtype, na_value + result_dtype = np.dtype(np.object_) + return result_dtype, na_value From 7a8b93a5462180f574afccfda3bc01c252b66574 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 8 Sep 2025 10:58:54 +0200 Subject: [PATCH 2/4] fixup --- pandas/core/arrays/_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/_utils.py b/pandas/core/arrays/_utils.py index 4a57183f22a74..05978acc8586b 100644 --- a/pandas/core/arrays/_utils.py +++ b/pandas/core/arrays/_utils.py @@ -45,13 +45,13 @@ def to_numpy_dtype_inference( result_dtype = None if na_value is lib.no_default: - if dtype is None or not hasna: + if result_dtype is None or not hasna: na_value = arr.dtype.na_value - elif dtype.kind == "f": # type: ignore[union-attr] + elif result_dtype.kind == "f": na_value = np.nan - elif dtype.kind == "M": # type: ignore[union-attr] + elif result_dtype.kind == "M": na_value = np.datetime64("nat") - elif dtype.kind == "m": # type: ignore[union-attr] + elif result_dtype.kind == "m": na_value = np.timedelta64("nat") else: na_value = arr.dtype.na_value From 0710ea4d1425e594a565acd9c6ec7949125ddef9 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 8 Sep 2025 13:45:21 +0200 Subject: [PATCH 3/4] add back ignore for numpy_dtype --- pandas/core/arrays/_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/arrays/_utils.py b/pandas/core/arrays/_utils.py index 05978acc8586b..a5656de717e10 100644 --- a/pandas/core/arrays/_utils.py +++ b/pandas/core/arrays/_utils.py @@ -34,11 +34,11 @@ def to_numpy_dtype_inference( if arr.dtype.kind in "iu": result_dtype = np.dtype(np.float64) else: - result_dtype = arr.dtype.numpy_dtype + result_dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] if na_value is lib.no_default: na_value = np.nan else: - result_dtype = arr.dtype.numpy_dtype + result_dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] elif dtype is not None: result_dtype = np.dtype(dtype) else: From f72c7e682fe3203490b19fd202b09da379348f22 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 8 Sep 2025 13:47:00 +0200 Subject: [PATCH 4/4] input is always EA --- pandas/core/arrays/_utils.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/_utils.py b/pandas/core/arrays/_utils.py index a5656de717e10..4957fb0fa2069 100644 --- a/pandas/core/arrays/_utils.py +++ b/pandas/core/arrays/_utils.py @@ -15,13 +15,14 @@ if TYPE_CHECKING: from pandas._typing import ( - ArrayLike, npt, ) + from pandas.core.arrays.base import ExtensionArray + def to_numpy_dtype_inference( - arr: ArrayLike, dtype: npt.DTypeLike | None, na_value, hasna: bool + arr: ExtensionArray, dtype: npt.DTypeLike | None, na_value, hasna: bool ) -> tuple[np.dtype | None, Any]: result_dtype: np.dtype | None inferred_numeric_dtype = False @@ -34,11 +35,11 @@ def to_numpy_dtype_inference( if arr.dtype.kind in "iu": result_dtype = np.dtype(np.float64) else: - result_dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] + result_dtype = arr.dtype.numpy_dtype # type: ignore[attr-defined] if na_value is lib.no_default: na_value = np.nan else: - result_dtype = arr.dtype.numpy_dtype # type: ignore[union-attr] + result_dtype = arr.dtype.numpy_dtype # type: ignore[attr-defined] elif dtype is not None: result_dtype = np.dtype(dtype) else: