Skip to content

Commit 441af3e

Browse files
Add NpDtype for functions only allow numpy dtypes
1 parent ae5722b commit 441af3e

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

pandas/_typing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@
9494
Axes = Collection
9595

9696
# dtypes
97-
Dtype = Union[
98-
"ExtensionDtype", str, np.dtype, Type[Union[str, float, int, complex, bool, object]]
99-
]
97+
NpDtype = Union[str, np.dtype, Type[Union[str, float, int, complex, bool, object]]]
98+
Dtype = Union["ExtensionDtype", NpDtype]
10099
# DtypeArg specifies all allowable dtypes in a functions its dtype argument
101100
DtypeArg = Union[Dtype, Dict[Label, Dtype]]
102101
DtypeObj = Union[np.dtype, "ExtensionDtype"]

pandas/core/arrays/numpy_.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from numpy.lib.mixins import NDArrayOperatorsMixin
66

77
from pandas._libs import lib
8-
from pandas._typing import Dtype, Scalar
8+
from pandas._typing import Dtype, NpDtype, Scalar
99
from pandas.compat.numpy import function as nv
1010

1111
from pandas.core.dtypes.dtypes import ExtensionDtype
@@ -199,7 +199,7 @@ def dtype(self) -> PandasDtype:
199199
# ------------------------------------------------------------------------
200200
# NumPy Array Interface
201201

202-
def __array__(self, dtype=None) -> np.ndarray:
202+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
203203
return np.asarray(self._ndarray, dtype=dtype)
204204

205205
_HANDLED_TYPES = (np.ndarray, numbers.Number)

pandas/core/arrays/period.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
get_period_field_arr,
2727
period_asfreq_arr,
2828
)
29-
from pandas._typing import AnyArrayLike, Dtype
29+
from pandas._typing import AnyArrayLike, Dtype, NpDtype
3030
from pandas.util._decorators import cache_readonly, doc
3131

3232
from pandas.core.dtypes.common import (
@@ -304,7 +304,7 @@ def freq(self) -> BaseOffset:
304304
"""
305305
return self.dtype.freq
306306

307-
def __array__(self, dtype=None) -> np.ndarray:
307+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
308308
if dtype == "i8":
309309
return self.asi8
310310
elif dtype == bool:

pandas/core/indexes/base.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@
2727
from pandas._libs.lib import is_datetime_array, no_default
2828
from pandas._libs.tslibs import IncompatibleFrequency, OutOfBoundsDatetime, Timestamp
2929
from pandas._libs.tslibs.timezones import tz_compare
30-
from pandas._typing import AnyArrayLike, ArrayLike, Dtype, DtypeObj, Label, Shape, final
30+
from pandas._typing import (
31+
AnyArrayLike,
32+
ArrayLike,
33+
Dtype,
34+
DtypeObj,
35+
Label,
36+
NpDtype,
37+
Shape,
38+
final,
39+
)
3140
from pandas.compat.numpy import function as nv
3241
from pandas.errors import DuplicateLabelError, InvalidIndexError
3342
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -619,7 +628,7 @@ def __len__(self) -> int:
619628
"""
620629
return len(self._data)
621630

622-
def __array__(self, dtype=None) -> np.ndarray:
631+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
623632
"""
624633
The array interface, return my values.
625634
"""

pandas/core/indexes/multi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from pandas._libs import algos as libalgos, index as libindex, lib
2222
from pandas._libs.hashtable import duplicated_int64
23-
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, Scalar, Shape
23+
from pandas._typing import AnyArrayLike, Dtype, DtypeObj, Label, NpDtype, Scalar, Shape
2424
from pandas.compat.numpy import function as nv
2525
from pandas.errors import InvalidIndexError, PerformanceWarning, UnsortedIndexError
2626
from pandas.util._decorators import Appender, cache_readonly, doc
@@ -1204,7 +1204,7 @@ def copy(
12041204
new_index = new_index.astype(dtype)
12051205
return new_index
12061206

1207-
def __array__(self, dtype=None) -> np.ndarray:
1207+
def __array__(self, dtype: Optional[NpDtype] = None) -> np.ndarray:
12081208
""" the array interface, return my values """
12091209
return self.values
12101210

0 commit comments

Comments
 (0)