Skip to content

Commit 50a59ba

Browse files
TYP: make numpy.typing available from pandas._typing (#41945)
1 parent fa0052e commit 50a59ba

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

pandas/_typing.py

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
final,
4343
)
4444

45+
import numpy.typing as npt
46+
4547
from pandas._libs import (
4648
Period,
4749
Timedelta,
@@ -73,6 +75,7 @@
7375
from pandas.io.formats.format import EngFormatter
7476
from pandas.tseries.offsets import DateOffset
7577
else:
78+
npt: Any = None
7679
# typing.final does not exist until py38
7780
final = lambda x: x
7881
# typing.TypedDict does not exist until py38

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@
6363
IndexKeyFunc,
6464
IndexLabel,
6565
Level,
66-
NpDtype,
6766
PythonFuncType,
6867
Renamer,
6968
Scalar,
7069
StorageOptions,
7170
Suffixes,
7271
ValueKeyFunc,
72+
npt,
7373
)
7474
from pandas.compat._optional import import_optional_dependency
7575
from pandas.compat.numpy import function as nv
@@ -1593,7 +1593,7 @@ def from_dict(
15931593

15941594
def to_numpy(
15951595
self,
1596-
dtype: NpDtype | None = None,
1596+
dtype: npt.DTypeLike | None = None,
15971597
copy: bool = False,
15981598
na_value=lib.no_default,
15991599
) -> np.ndarray:

pandas/core/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
JSONSerializable,
4747
Level,
4848
Manager,
49-
NpDtype,
5049
RandomState,
5150
Renamer,
5251
StorageOptions,
@@ -55,6 +54,7 @@
5554
TimestampConvertibleTypes,
5655
ValueKeyFunc,
5756
final,
57+
npt,
5858
)
5959
from pandas.compat._optional import import_optional_dependency
6060
from pandas.compat.numpy import function as nv
@@ -1988,7 +1988,7 @@ def empty(self) -> bool_t:
19881988
# GH#23114 Ensure ndarray.__op__(DataFrame) returns NotImplemented
19891989
__array_priority__ = 1000
19901990

1991-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
1991+
def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
19921992
return np.asarray(self._values, dtype=dtype)
19931993

19941994
def __array_wrap__(

pandas/core/internals/managers.py

+26-11
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
from pandas._libs.internals import BlockPlacement
2323
from pandas._typing import (
2424
ArrayLike,
25-
Dtype,
2625
DtypeObj,
2726
Shape,
27+
npt,
2828
type_t,
2929
)
3030
from pandas.errors import PerformanceWarning
@@ -1389,7 +1389,7 @@ def to_dict(self, copy: bool = True):
13891389
def as_array(
13901390
self,
13911391
transpose: bool = False,
1392-
dtype: Dtype | None = None,
1392+
dtype: npt.DTypeLike | None = None,
13931393
copy: bool = False,
13941394
na_value=lib.no_default,
13951395
) -> np.ndarray:
@@ -1429,17 +1429,21 @@ def as_array(
14291429
# error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
14301430
# attribute "to_numpy"
14311431
arr = blk.values.to_numpy( # type: ignore[union-attr]
1432-
dtype=dtype, na_value=na_value
1432+
# pandas/core/internals/managers.py:1428: error: Argument "dtype" to
1433+
# "to_numpy" of "ExtensionArray" has incompatible type
1434+
# "Optional[Union[dtype[Any], None, type, _SupportsDType, str,
1435+
# Union[Tuple[Any, int], Tuple[Any, Union[SupportsIndex,
1436+
# Sequence[SupportsIndex]]], List[Any], _DTypeDict, Tuple[Any,
1437+
# Any]]]]"; expected "Optional[Union[ExtensionDtype, Union[str,
1438+
# dtype[Any]], Type[str], Type[float], Type[int], Type[complex],
1439+
# Type[bool], Type[object]]]"
1440+
dtype=dtype, # type: ignore[arg-type]
1441+
na_value=na_value,
14331442
).reshape(blk.shape)
14341443
else:
14351444
arr = np.asarray(blk.get_values())
14361445
if dtype:
1437-
# error: Argument 1 to "astype" of "_ArrayOrScalarCommon" has
1438-
# incompatible type "Union[ExtensionDtype, str, dtype[Any],
1439-
# Type[object]]"; expected "Union[dtype[Any], None, type,
1440-
# _SupportsDType, str, Union[Tuple[Any, int], Tuple[Any, Union[int,
1441-
# Sequence[int]]], List[Any], _DTypeDict, Tuple[Any, Any]]]"
1442-
arr = arr.astype(dtype, copy=False) # type: ignore[arg-type]
1446+
arr = arr.astype(dtype, copy=False)
14431447
else:
14441448
arr = self._interleave(dtype=dtype, na_value=na_value)
14451449
# The underlying data was copied within _interleave
@@ -1454,7 +1458,9 @@ def as_array(
14541458
return arr.transpose() if transpose else arr
14551459

14561460
def _interleave(
1457-
self, dtype: Dtype | None = None, na_value=lib.no_default
1461+
self,
1462+
dtype: npt.DTypeLike | ExtensionDtype | None = None,
1463+
na_value=lib.no_default,
14581464
) -> np.ndarray:
14591465
"""
14601466
Return ndarray from blocks with specified item order
@@ -1489,7 +1495,16 @@ def _interleave(
14891495
# error: Item "ndarray" of "Union[ndarray, ExtensionArray]" has no
14901496
# attribute "to_numpy"
14911497
arr = blk.values.to_numpy( # type: ignore[union-attr]
1492-
dtype=dtype, na_value=na_value
1498+
# pandas/core/internals/managers.py:1485: error: Argument "dtype" to
1499+
# "to_numpy" of "ExtensionArray" has incompatible type
1500+
# "Union[dtype[Any], None, type, _SupportsDType, str, Tuple[Any,
1501+
# Union[SupportsIndex, Sequence[SupportsIndex]]], List[Any],
1502+
# _DTypeDict, Tuple[Any, Any], ExtensionDtype]"; expected
1503+
# "Optional[Union[ExtensionDtype, Union[str, dtype[Any]], Type[str],
1504+
# Type[float], Type[int], Type[complex], Type[bool], Type[object]]]"
1505+
# [arg-type]
1506+
dtype=dtype, # type: ignore[arg-type]
1507+
na_value=na_value,
14931508
)
14941509
else:
14951510
# error: Argument 1 to "get_values" of "Block" has incompatible type

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
FillnaOptions,
4242
FrameOrSeriesUnion,
4343
IndexKeyFunc,
44-
NpDtype,
4544
SingleManager,
4645
StorageOptions,
4746
ValueKeyFunc,
47+
npt,
4848
)
4949
from pandas.compat.numpy import function as nv
5050
from pandas.errors import InvalidIndexError
@@ -808,7 +808,7 @@ def view(self, dtype: Dtype | None = None) -> Series:
808808
# NDArray Compat
809809
_HANDLED_TYPES = (Index, ExtensionArray, np.ndarray)
810810

811-
def __array__(self, dtype: NpDtype | None = None) -> np.ndarray:
811+
def __array__(self, dtype: npt.DTypeLike | None = None) -> np.ndarray:
812812
"""
813813
Return the values as a NumPy array.
814814

0 commit comments

Comments
 (0)