diff --git a/pandas/core/array_algos/putmask.py b/pandas/core/array_algos/putmask.py index 3daf1b3ae3902..7f811cc987d34 100644 --- a/pandas/core/array_algos/putmask.py +++ b/pandas/core/array_algos/putmask.py @@ -191,7 +191,7 @@ def extract_bool_array(mask: ArrayLike) -> np.ndarray: # We could have BooleanArray, Sparse[bool], ... # Except for BooleanArray, this is equivalent to just # np.asarray(mask, dtype=bool) - mask = mask.to_numpy(dtype=bool, na_value=False) + mask = mask.to_numpy(dtype=np.dtype("bool"), na_value=False) mask = np.asarray(mask, dtype=bool) return mask diff --git a/pandas/core/arrays/base.py b/pandas/core/arrays/base.py index 7dddb9f3d6f25..73a5259fe6216 100644 --- a/pandas/core/arrays/base.py +++ b/pandas/core/arrays/base.py @@ -26,6 +26,7 @@ ArrayLike, Dtype, FillnaOptions, + NpDtype, PositionalIndexer, Shape, ) @@ -429,7 +430,7 @@ def __ne__(self, other: Any) -> ArrayLike: # type: ignore[override] def to_numpy( self, - dtype: Dtype | None = None, + dtype: NpDtype | None = None, copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: @@ -458,12 +459,7 @@ def to_numpy( ------- numpy.ndarray """ - # error: Argument "dtype" to "asarray" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int], - # Type[complex], Type[bool], Type[object], None]"; expected "Union[dtype[Any], - # None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int, - # Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" - result = np.asarray(self, dtype=dtype) # type: ignore[arg-type] + result = np.asarray(self, dtype=dtype) if copy or na_value is not lib.no_default: result = result.copy() if na_value is not lib.no_default: diff --git a/pandas/core/arrays/masked.py b/pandas/core/arrays/masked.py index 11f9f645920ec..d7c746ba29677 100644 --- a/pandas/core/arrays/masked.py +++ b/pandas/core/arrays/masked.py @@ -208,10 +208,7 @@ def __len__(self) -> int: def __invert__(self: BaseMaskedArrayT) -> BaseMaskedArrayT: return type(self)(~self._data, self._mask.copy()) - # error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray"; - # supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any], - # Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]" - def to_numpy( # type: ignore[override] + def to_numpy( self, dtype: NpDtype | None = None, copy: bool = False, diff --git a/pandas/core/arrays/numpy_.py b/pandas/core/arrays/numpy_.py index e9d554200805e..2f34e2b0fb588 100644 --- a/pandas/core/arrays/numpy_.py +++ b/pandas/core/arrays/numpy_.py @@ -367,10 +367,7 @@ def skew( # ------------------------------------------------------------------------ # Additional Methods - # error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray"; - # supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any], - # Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]" - def to_numpy( # type: ignore[override] + def to_numpy( self, dtype: NpDtype | None = None, copy: bool = False, diff --git a/pandas/core/arrays/string_arrow.py b/pandas/core/arrays/string_arrow.py index 3cf471e381da9..26ca9f9bd6023 100644 --- a/pandas/core/arrays/string_arrow.py +++ b/pandas/core/arrays/string_arrow.py @@ -275,10 +275,7 @@ def __arrow_array__(self, type=None): """Convert myself to a pyarrow Array or ChunkedArray.""" return self._data - # error: Argument 1 of "to_numpy" is incompatible with supertype "ExtensionArray"; - # supertype defines the argument type as "Union[ExtensionDtype, str, dtype[Any], - # Type[str], Type[float], Type[int], Type[complex], Type[bool], Type[object], None]" - def to_numpy( # type: ignore[override] + def to_numpy( self, dtype: NpDtype | None = None, copy: bool = False, diff --git a/pandas/core/base.py b/pandas/core/base.py index 55e776d2e6b73..7ade73c5c5f0a 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -19,10 +19,10 @@ import pandas._libs.lib as lib from pandas._typing import ( ArrayLike, - Dtype, DtypeObj, FrameOrSeries, IndexLabel, + NpDtype, Shape, final, ) @@ -413,7 +413,7 @@ def array(self) -> ExtensionArray: def to_numpy( self, - dtype: Dtype | None = None, + dtype: NpDtype | None = None, copy: bool = False, na_value=lib.no_default, **kwargs, @@ -523,12 +523,7 @@ def to_numpy( f"to_numpy() got an unexpected keyword argument '{bad_keys}'" ) - # error: Argument "dtype" to "asarray" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[str], Type[float], Type[int], - # Type[complex], Type[bool], Type[object], None]"; expected "Union[dtype[Any], - # None, type, _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int, - # Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" - result = np.asarray(self._values, dtype=dtype) # type: ignore[arg-type] + result = np.asarray(self._values, dtype=dtype) # TODO(GH-24345): Avoid potential double copy if copy or na_value is not lib.no_default: result = result.copy() diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index b00a1160fb01b..a6ba850bcedfa 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2424,12 +2424,12 @@ def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, np.dtype | None]: inference: np.dtype | None = None if is_integer_dtype(vals.dtype): if isinstance(vals, ExtensionArray): - out = vals.to_numpy(dtype=float, na_value=np.nan) + out = vals.to_numpy(dtype="float64", na_value=np.nan) else: out = vals inference = np.dtype(np.int64) elif is_bool_dtype(vals.dtype) and isinstance(vals, ExtensionArray): - out = vals.to_numpy(dtype=float, na_value=np.nan) + out = vals.to_numpy(dtype="float64", na_value=np.nan) elif is_datetime64_dtype(vals.dtype): inference = np.dtype("datetime64[ns]") out = np.asarray(vals).astype(float) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 323aa45874d96..e77d39c53ea36 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -24,6 +24,7 @@ ArrayLike, Dtype, DtypeObj, + NpDtype, Shape, type_t, ) @@ -1385,7 +1386,7 @@ def to_dict(self, copy: bool = True): def as_array( self, transpose: bool = False, - dtype: Dtype | None = None, + dtype: NpDtype | None = None, copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: @@ -1396,7 +1397,7 @@ def as_array( ---------- transpose : bool, default False If True, transpose the return array. - dtype : object, default None + dtype : str or numpy.dtype, optional Data type of the return array. copy : bool, default False If True then guarantee that a copy is returned. A value of @@ -1430,12 +1431,7 @@ def as_array( else: arr = np.asarray(blk.get_values()) if dtype: - # error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has - # incompatible type "Union[ExtensionDtype, str, dtype[Any], - # Type[object]]"; expected "Union[dtype[Any], None, type, - # _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int, - # Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]" - arr = arr.astype(dtype, copy=False) # type: ignore[arg-type] + arr = arr.astype(dtype, copy=False) else: arr = self._interleave(dtype=dtype, na_value=na_value) # The underlying data was copied within _interleave @@ -1468,12 +1464,9 @@ def _interleave( elif is_dtype_equal(dtype, str): dtype = np.dtype("object") - # error: Argument "dtype" to "empty" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], None, type, _SupportsDType, str, Union[Tuple[Any, int], - # Tuple[Any, Union[int, Sequence[int]]], List[Any], _DTypeDict, - # Tuple[Any, Any]]]" - result = np.empty(self.shape, dtype=dtype) # type: ignore[arg-type] + dtype = cast(np.dtype, dtype) + + result = np.empty(self.shape, dtype=dtype) itemmask = np.zeros(self.shape[0]) @@ -1488,10 +1481,7 @@ def _interleave( dtype=dtype, na_value=na_value ) else: - # error: Argument 1 to "get_values" of "Block" has incompatible type - # "Union[ExtensionDtype, str, dtype[Any], Type[object], None]"; expected - # "Union[dtype[Any], ExtensionDtype, None]" - arr = blk.get_values(dtype) # type: ignore[arg-type] + arr = blk.get_values(dtype) result[rl.indexer] = arr itemmask[rl.indexer] = 1