diff --git a/pandas/_typing.py b/pandas/_typing.py index a9852dd4b13cf..ccf7699fb0b4b 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -42,6 +42,8 @@ final, ) + import numpy.typing as npt + from pandas._libs import ( Period, Timedelta, @@ -73,6 +75,7 @@ from pandas.io.formats.format import EngFormatter from pandas.tseries.offsets import DateOffset else: + npt: Any = None # typing.final does not exist until py38 final = lambda x: x # typing.TypedDict does not exist until py38 diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 0acbb0c34266f..d61b818195d08 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -63,13 +63,13 @@ IndexKeyFunc, IndexLabel, Level, - NpDtype, PythonFuncType, Renamer, Scalar, StorageOptions, Suffixes, ValueKeyFunc, + npt, ) from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv @@ -1593,7 +1593,7 @@ def from_dict( def to_numpy( self, - dtype: NpDtype | None = None, + dtype: npt.DTypeLike | None = None, copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index adc722e770cff..1f9bcf52cb938 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -46,7 +46,6 @@ JSONSerializable, Level, Manager, - NpDtype, RandomState, Renamer, StorageOptions, @@ -55,6 +54,7 @@ TimestampConvertibleTypes, ValueKeyFunc, final, + npt, ) from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv @@ -1988,7 +1988,7 @@ def empty(self) -> bool_t: # GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented __array_priority__ = 1000 - def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: + def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray: return np.asarray(self._values, dtype=dtype) def __array_wrap__( diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 48f0b7f7f964b..f1358be043d27 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -22,9 +22,9 @@ from pandas._libs.internals import BlockPlacement from pandas._typing import ( ArrayLike, - Dtype, DtypeObj, Shape, + npt, type_t, ) from pandas.errors import PerformanceWarning @@ -1385,7 +1385,7 @@ def to_dict(self, copy: bool = True): def as_array( self, transpose: bool = False, - dtype: Dtype | None = None, + dtype: npt.DTypeLike | None = None, copy: bool = False, na_value=lib.no_default, ) -> np.ndarray: @@ -1425,17 +1425,21 @@ def as_array( # error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no # attribute "to_numpy" arr = blk.values.to_numpy( # type: ignore[union-attr] - dtype=dtype, na_value=na_value + # pandas/core/internals/managers.py:1428: error: Argument "dtype" to + # "to_numpy" of "ExtensionArray" has incompatible type + # "Optional[Union[dtype[Any], None, type, _SupportsDType, str, + # Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex, + # Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any, + # Any]]]]"; expected "Optional[Union[ExtensionDtype, Union[str, + # dtype[Any]], Type[str], Type[float], Type[int], Type[complex], + # Type[bool], Type[object]]]" + dtype=dtype, # type: ignore[arg-type] + na_value=na_value, ).reshape(blk.shape) 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 @@ -1450,7 +1454,9 @@ def as_array( return arr.transpose() if transpose else arr def _interleave( - self, dtype: Dtype | None = None, na_value=lib.no_default + self, + dtype: npt.DTypeLike | ExtensionDtype | None = None, + na_value=lib.no_default, ) -> np.ndarray: """ Return ndarray from blocks with specified item order @@ -1485,7 +1491,16 @@ def _interleave( # error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no # attribute "to_numpy" arr = blk.values.to_numpy( # type: ignore[union-attr] - dtype=dtype, na_value=na_value + # pandas/core/internals/managers.py:1485: error: Argument "dtype" to + # "to_numpy" of "ExtensionArray" has incompatible type + # "Union[dtype[Any], None, type, _SupportsDType, str, Tuple[Any, + # Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any], + # _DTypeDict, Tuple[Any, Any], ExtensionDtype]"; expected + # "Optional[Union[ExtensionDtype, Union[str, dtype[Any]], Type[str], + # Type[float], Type[int], Type[complex], Type[bool], Type[object]]]" + # [arg-type] + dtype=dtype, # type: ignore[arg-type] + na_value=na_value, ) else: # error: Argument 1 to "get_values" of "Block" has incompatible type diff --git a/pandas/core/series.py b/pandas/core/series.py index c20e09e9eac5d..def938861a775 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -41,10 +41,10 @@ FillnaOptions, FrameOrSeriesUnion, IndexKeyFunc, - NpDtype, SingleManager, StorageOptions, ValueKeyFunc, + npt, ) from pandas.compat.numpy import function as nv from pandas.errors import InvalidIndexError @@ -808,7 +808,7 @@ def view(self, dtype: Dtype | None = None) -> Series: # NDArray Compat _HANDLED_TYPES = (Index, ExtensionArray, np.ndarray) - def __array__(self, dtype: NpDtype | None = None) -> np.ndarray: + def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray: """ Return the values as a NumPy array.