From 5804098e153da39ac4c1180e3c43a5545a1564e7 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 2 Nov 2021 20:22:34 +0000 Subject: [PATCH 1/3] overload infer_dtype_from_array --- pandas/core/algorithms.py | 6 +----- pandas/core/dtypes/cast.py | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index c1b587ce3a6b2..6708096d4f528 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -1723,11 +1723,7 @@ def safe_sort( if not isinstance(values, (np.ndarray, ABCExtensionArray)): # don't convert to string types dtype, _ = infer_dtype_from_array(values) - # error: Argument "dtype" to "asarray" has incompatible type "Union[dtype[Any], - # ExtensionDtype]"; expected "Union[dtype[Any], None, type, _SupportsDType, str, - # Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any], - # _DTypeDict, Tuple[Any, Any]]]" - values = np.asarray(values, dtype=dtype) # type: ignore[arg-type] + values = np.asarray(values, dtype=dtype) sorter = None diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index c0ac9098ec7fc..6b4fe68d65a8b 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -14,6 +14,7 @@ from typing import ( TYPE_CHECKING, Any, + Literal, Sized, TypeVar, cast, @@ -795,6 +796,25 @@ def dict_compat(d: dict[Scalar, Scalar]) -> dict[Scalar, Scalar]: return {maybe_box_datetimelike(key): value for key, value in d.items()} +@overload +def infer_dtype_from_array( + arr, +) -> tuple[np.dtype, ArrayLike]: + ... + + +@overload +def infer_dtype_from_array( + arr, pandas_dtype: Literal[False] = ... +) -> tuple[np.dtype, ArrayLike]: + ... + + +@overload +def infer_dtype_from_array(arr, pandas_dtype: bool = ...) -> tuple[DtypeObj, ArrayLike]: + ... + + def infer_dtype_from_array( arr, pandas_dtype: bool = False ) -> tuple[DtypeObj, ArrayLike]: From 2caa77dc7e3f17bca3893e6c0673332fe6431241 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 2 Nov 2021 20:41:22 +0000 Subject: [PATCH 2/3] is_complex_dtype in _ensure_data --- pandas/core/algorithms.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 6708096d4f528..2876921824c27 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -165,10 +165,8 @@ def _ensure_data(values: ArrayLike) -> np.ndarray: return np.asarray(values) elif is_complex_dtype(values.dtype): - # Incompatible return value type (got "Tuple[Union[Any, ExtensionArray, - # ndarray[Any, Any]], Union[Any, ExtensionDtype]]", expected - # "Tuple[ndarray[Any, Any], Union[dtype[Any], ExtensionDtype]]") - return values # type: ignore[return-value] + assert isinstance(values, np.ndarray) # for mypy + return values # datetimelike elif needs_i8_conversion(values.dtype): From 4f9a72dc803b48d0c493bac335d650423ab27d47 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 2 Nov 2021 21:05:31 +0000 Subject: [PATCH 3/3] update _ensure_data docstring --- pandas/core/algorithms.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/algorithms.py b/pandas/core/algorithms.py index 2876921824c27..011f9c2a680cb 100644 --- a/pandas/core/algorithms.py +++ b/pandas/core/algorithms.py @@ -112,16 +112,19 @@ # --------------- # def _ensure_data(values: ArrayLike) -> np.ndarray: """ - routine to ensure that our data is of the correct - input dtype for lower-level routines + Ensure values is of the correct input dtype for lower-level routines. This will coerce: - ints -> int64 - uint -> uint64 - - bool -> uint64 (TODO this should be uint8) + - bool -> uint8 - datetimelike -> i8 - datetime64tz -> i8 (in local tz) - categorical -> codes + - categorical[bool] without nulls -> uint8 + - categorical[bool] with nulls -> ValueError: cannot convert float NaN to integer + - boolean without nulls -> uint8 + - boolean with nulls -> object Parameters ----------