Skip to content

Commit

Permalink
TYP: make numpy.typing available from pandas._typing (#41945)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjayhawkins committed Jun 25, 2021
1 parent fa0052e commit 50a59ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
3 changes: 3 additions & 0 deletions pandas/_typing.py
Expand Up @@ -42,6 +42,8 @@
final,
)

import numpy.typing as npt

from pandas._libs import (
Period,
Timedelta,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/generic.py
Expand Up @@ -46,7 +46,6 @@
JSONSerializable,
Level,
Manager,
NpDtype,
RandomState,
Renamer,
StorageOptions,
Expand All @@ -55,6 +54,7 @@
TimestampConvertibleTypes,
ValueKeyFunc,
final,
npt,
)
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -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__(
Expand Down
37 changes: 26 additions & 11 deletions pandas/core/internals/managers.py
Expand Up @@ -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
Expand Down Expand Up @@ -1389,7 +1389,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:
Expand Down Expand Up @@ -1429,17 +1429,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
Expand All @@ -1454,7 +1458,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
Expand Down Expand Up @@ -1489,7 +1495,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
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 50a59ba

Please sign in to comment.