From f44be76b1dce6d953b9d39a7729126449dbdef01 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Fri, 11 Jun 2021 10:43:41 +0100 Subject: [PATCH] TYP: make numpy.typing available from pandas._typing --- pandas/_typing.py | 3 +++ pandas/core/apply.py | 6 +---- pandas/core/frame.py | 4 ++-- pandas/core/generic.py | 10 +++------ pandas/core/internals/managers.py | 37 ++++++++++++++++++++++--------- pandas/core/series.py | 4 ++-- 6 files changed, 37 insertions(+), 27 deletions(-) diff --git a/pandas/_typing.py b/pandas/_typing.py index 12d23786c3387..c7e9a23c6b31b 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/apply.py b/pandas/core/apply.py index 388c1881afed7..70cfdc0060341 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1062,11 +1062,7 @@ def apply_standard(self) -> FrameOrSeriesUnion: with np.errstate(all="ignore"): if isinstance(f, np.ufunc): - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "Series"; expected "Union[Union[int, float, complex, str, bytes, - # generic], Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - return f(obj) # type: ignore[arg-type] + return f(obj) # row-wise access if is_extension_array_dtype(obj.dtype) and hasattr(obj._values, "map"): diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 91b9bdd564676..2115689a56c27 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 99e4888d08be6..d3326c53718af 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -46,7 +46,6 @@ JSONSerializable, Level, Manager, - NpDtype, Renamer, StorageOptions, T, @@ -54,6 +53,7 @@ TimestampConvertibleTypes, ValueKeyFunc, final, + npt, ) from pandas.compat._optional import import_optional_dependency from pandas.compat.numpy import function as nv @@ -1987,7 +1987,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__( @@ -9751,11 +9751,7 @@ def abs(self: FrameOrSeries) -> FrameOrSeries: 2 6 30 -30 3 7 40 -50 """ - # error: Argument 1 to "__call__" of "ufunc" has incompatible type - # "FrameOrSeries"; expected "Union[Union[int, float, complex, str, bytes, - # generic], Sequence[Union[int, float, complex, str, bytes, generic]], - # Sequence[Sequence[Any]], _SupportsArray]" - return np.abs(self) # type: ignore[arg-type] + return np.abs(self) @final def describe( 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 59ea6710ea6cd..413e1acbb2172 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 @@ -810,7 +810,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.