Skip to content

Commit f2f3d27

Browse files
committed
reduce ndarray
1 parent 84f8d36 commit f2f3d27

35 files changed

+427
-346
lines changed

pandas-stubs/_libs/interval.pyi

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ from typing import (
44
Literal,
55
TypeVar,
66
overload,
7+
type_check_only,
78
)
89

910
import numpy as np
@@ -18,7 +19,6 @@ from pandas._typing import (
1819
IntervalClosedType,
1920
IntervalT,
2021
np_1darray,
21-
npt,
2222
)
2323

2424
VALID_CLOSED: frozenset[str]
@@ -27,6 +27,7 @@ _OrderableScalarT = TypeVar("_OrderableScalarT", bound=int | float)
2727
_OrderableTimesT = TypeVar("_OrderableTimesT", bound=Timestamp | Timedelta)
2828
_OrderableT = TypeVar("_OrderableT", bound=int | float | Timestamp | Timedelta)
2929

30+
@type_check_only
3031
class _LengthDescriptor:
3132
@overload
3233
def __get__(
@@ -36,18 +37,15 @@ class _LengthDescriptor:
3637
def __get__(
3738
self, instance: Interval[_OrderableTimesT], owner: Any
3839
) -> Timedelta: ...
39-
@overload
40-
def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ...
4140

41+
@type_check_only
4242
class _MidDescriptor:
4343
@overload
4444
def __get__(self, instance: Interval[_OrderableScalarT], owner: Any) -> float: ...
4545
@overload
4646
def __get__(
4747
self, instance: Interval[_OrderableTimesT], owner: Any
4848
) -> _OrderableTimesT: ...
49-
@overload
50-
def __get__(self, instance: IntervalTree, owner: Any) -> np.ndarray: ...
5149

5250
class IntervalMixin:
5351
@property
@@ -68,8 +66,8 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
6866
def right(self: Interval[_OrderableT]) -> _OrderableT: ...
6967
@property
7068
def closed(self) -> IntervalClosedType: ...
71-
mid: _MidDescriptor
72-
length: _LengthDescriptor
69+
mid = _MidDescriptor()
70+
length = _LengthDescriptor()
7371
def __init__(
7472
self,
7573
left: _OrderableT,
@@ -223,21 +221,4 @@ class Interval(IntervalMixin, Generic[_OrderableT]):
223221
@overload
224222
def __ne__(self, other: object) -> Literal[True]: ...
225223

226-
class IntervalTree(IntervalMixin):
227-
def __init__(
228-
self,
229-
left: np.ndarray,
230-
right: np.ndarray,
231-
closed: IntervalClosedType = ...,
232-
leaf_size: int = ...,
233-
) -> None: ...
234-
def get_indexer(self, target) -> npt.NDArray[np.intp]: ...
235-
def get_indexer_non_unique(
236-
self, target
237-
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]: ...
238-
_na_count: int
239-
@property
240-
def is_overlapping(self) -> bool: ...
241-
@property
242-
def is_monotonic_increasing(self) -> bool: ...
243-
def clear_mapping(self) -> None: ...
224+
class IntervalTree(IntervalMixin): ...

pandas-stubs/_libs/tslibs/offsets.pyi

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ from typing import (
1414

1515
from dateutil.relativedelta import weekday as WeekdayClass
1616
import numpy as np
17+
from numpy import typing as npt
1718
from pandas import Timestamp
1819
from typing_extensions import Self
1920

20-
from pandas._typing import npt
21+
from pandas._typing import (
22+
ShapeT,
23+
np_ndarray,
24+
)
2125

2226
from pandas.tseries.holiday import AbstractHolidayCalendar
2327

@@ -37,7 +41,9 @@ class BaseOffset:
3741
@property
3842
def base(self) -> BaseOffset: ...
3943
@overload
40-
def __add__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
44+
def __add__(
45+
self, other: np_ndarray[ShapeT, np.object_]
46+
) -> np_ndarray[ShapeT, np.object_]: ...
4147
@overload
4248
def __add__(self, other: _DatetimeT) -> _DatetimeT: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
4349
@overload
@@ -47,7 +53,9 @@ class BaseOffset:
4753
@overload
4854
def __add__(self, other: _TimedeltaT) -> _TimedeltaT: ...
4955
@overload
50-
def __radd__(self, other: npt.NDArray[np.object_]) -> npt.NDArray[np.object_]: ...
56+
def __radd__(
57+
self, other: np_ndarray[ShapeT, np.object_]
58+
) -> np_ndarray[ShapeT, np.object_]: ...
5159
@overload
5260
def __radd__(self, other: _DatetimeT) -> _DatetimeT: ... # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
5361
@overload
@@ -68,11 +76,15 @@ class BaseOffset:
6876
@overload
6977
def __rsub__(self, other: _TimedeltaT) -> _TimedeltaT: ...
7078
@overload
71-
def __mul__(self, other: np.ndarray) -> np.ndarray: ...
79+
def __mul__(
80+
self, other: np_ndarray[ShapeT, np.object_]
81+
) -> np_ndarray[ShapeT, np.object_]: ...
7282
@overload
7383
def __mul__(self, other: int) -> Self: ...
7484
@overload
75-
def __rmul__(self, other: np.ndarray) -> np.ndarray: ...
85+
def __rmul__(
86+
self, other: np_ndarray[ShapeT, np.object_]
87+
) -> np_ndarray[ShapeT, np.object_]: ...
7688
@overload
7789
def __rmul__(self, other: int) -> Self: ...
7890
def __neg__(self) -> Self: ...

pandas-stubs/_testing/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ from typing import (
1212
import warnings
1313

1414
from matplotlib.artist import Artist
15-
import numpy as np
15+
from numpy import typing as npt
1616
from pandas import (
1717
Categorical,
1818
DataFrame,
@@ -61,7 +61,7 @@ def assert_attr_equal(
6161
attr: str, left: object, right: object, obj: str = "Attributes"
6262
) -> None: ...
6363
def assert_is_valid_plot_return_object(
64-
objs: Series | np.ndarray | Artist | tuple | dict,
64+
objs: Series | npt.NDArray[Any] | Artist | tuple | dict,
6565
) -> None: ...
6666
def assert_is_sorted(seq: AnyArrayLike) -> None: ...
6767
def assert_categorical_equal(
@@ -96,7 +96,7 @@ def assert_extension_array_equal(
9696
left: ExtensionArray,
9797
right: ExtensionArray,
9898
check_dtype: bool | Literal["equiv"] = True,
99-
index_values: Index | np.ndarray | None = None,
99+
index_values: Index | npt.NDArray[Any] | None = None,
100100
check_exact: bool = False,
101101
rtol: float = 1e-5,
102102
atol: float = 1e-8,

pandas-stubs/_typing.pyi

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ HashableT5 = TypeVar("HashableT5", bound=Hashable)
8585

8686
# array-like
8787

88-
ArrayLike: TypeAlias = ExtensionArray | np.ndarray
88+
ArrayLike: TypeAlias = ExtensionArray | np.ndarray[tuple[int], np.dtype[Any]]
8989
AnyArrayLike: TypeAlias = ArrayLike | Index | Series
9090
AnyArrayLikeInt: TypeAlias = (
91-
IntegerArray | Index[int] | Series[int] | npt.NDArray[np.integer]
91+
IntegerArray
92+
| Index[int]
93+
| Series[int]
94+
| np.ndarray[tuple[int], np.dtype[np.integer]]
9295
)
9396

9497
# list-like
@@ -166,6 +169,7 @@ ToTimestampHow: TypeAlias = Literal["s", "e", "start", "end"]
166169
NDFrameT = TypeVar("NDFrameT", bound=NDFrame)
167170

168171
IndexT = TypeVar("IndexT", bound=Index)
172+
T_EXTENSION_ARRAY = TypeVar("T_EXTENSION_ARRAY", bound=ExtensionArray)
169173

170174
# From _typing.py, not used here:
171175
# FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex")
@@ -697,11 +701,15 @@ InterpolateOptions: TypeAlias = Literal[
697701
# Using List[int] here rather than Sequence[int] to disallow tuples.
698702

699703
ScalarIndexer: TypeAlias = int | np.integer
700-
SequenceIndexer: TypeAlias = slice | list[int] | np.ndarray
704+
SequenceIndexer: TypeAlias = (
705+
slice | list[int] | np.ndarray[tuple[int], np.dtype[np.intp]]
706+
)
701707
PositionalIndexer: TypeAlias = ScalarIndexer | SequenceIndexer
702708
PositionalIndexerTuple: TypeAlias = tuple[PositionalIndexer, PositionalIndexer]
703709
# PositionalIndexer2D = Union[PositionalIndexer, PositionalIndexerTuple] Not used in stubs
704-
TakeIndexer: TypeAlias = Sequence[int] | Sequence[np.integer] | npt.NDArray[np.integer]
710+
TakeIndexer: TypeAlias = (
711+
Sequence[int | np.integer] | np.ndarray[tuple[int], np.dtype[np.integer]]
712+
)
705713

706714
# Shared by functions such as drop and astype
707715
IgnoreRaise: TypeAlias = Literal["ignore", "raise"]
@@ -832,18 +840,6 @@ SliceType: TypeAlias = Hashable | None
832840
## All types below this point are only used in pandas-stubs
833841
######
834842

835-
num: TypeAlias = complex
836-
837-
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
838-
KeysArgType: TypeAlias = Any
839-
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
840-
ListLikeExceptSeriesAndStr: TypeAlias = (
841-
MutableSequence[Any] | np.ndarray | tuple[Any, ...] | Index
842-
)
843-
ListLikeU: TypeAlias = Sequence | np.ndarray | Series | Index
844-
ListLikeHashable: TypeAlias = (
845-
MutableSequence[HashableT] | np.ndarray | tuple[HashableT, ...] | range
846-
)
847843
StrLike: TypeAlias = str | np.str_
848844

849845
ScalarT = TypeVar("ScalarT", bound=Scalar)
@@ -871,6 +867,17 @@ np_ndarray: TypeAlias = np.ndarray[ShapeT, np.dtype[GenericT]]
871867
np_1darray: TypeAlias = np.ndarray[tuple[int], np.dtype[GenericT]]
872868
np_2darray: TypeAlias = np.ndarray[tuple[int, int], np.dtype[GenericT]]
873869

870+
DtypeNp = TypeVar("DtypeNp", bound=np.dtype[np.generic])
871+
KeysArgType: TypeAlias = Any
872+
ListLikeT = TypeVar("ListLikeT", bound=ListLike)
873+
ListLikeExceptSeriesAndStr: TypeAlias = (
874+
MutableSequence[Any] | np_1darray[Any] | tuple[Any, ...] | Index
875+
)
876+
ListLikeU: TypeAlias = Sequence | np_1darray[Any] | Series | Index
877+
ListLikeHashable: TypeAlias = (
878+
MutableSequence[HashableT] | np_1darray[Any] | tuple[HashableT, ...] | range
879+
)
880+
874881
class SupportsDType(Protocol[GenericT_co]):
875882
@property
876883
def dtype(self) -> np.dtype[GenericT_co]: ...

pandas-stubs/core/algorithms.pyi

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,73 @@ from typing import (
55
)
66

77
import numpy as np
8-
from pandas import (
9-
Categorical,
10-
CategoricalIndex,
11-
Index,
12-
IntervalIndex,
13-
PeriodIndex,
14-
Series,
15-
)
168
from pandas.api.extensions import ExtensionArray
9+
from pandas.core.arrays.categorical import Categorical
10+
from pandas.core.indexes.base import Index
11+
from pandas.core.indexes.category import CategoricalIndex
12+
from pandas.core.indexes.datetimes import DatetimeIndex
13+
from pandas.core.indexes.interval import IntervalIndex
14+
from pandas.core.indexes.period import PeriodIndex
15+
from pandas.core.series import Series
1716

1817
from pandas._typing import (
18+
T_EXTENSION_ARRAY,
1919
AnyArrayLike,
20+
GenericT,
2021
IntervalT,
2122
TakeIndexer,
2223
np_1darray,
2324
)
2425

2526
# These are type: ignored because the Index types overlap due to inheritance but indices
2627
# with extension types return the same type while standard type return ndarray
27-
28-
@overload
29-
def unique( # pyright: ignore[reportOverlappingOverload]
30-
values: PeriodIndex,
31-
) -> PeriodIndex: ...
3228
@overload
33-
def unique(values: CategoricalIndex) -> CategoricalIndex: ... # type: ignore[overload-overlap]
29+
def unique(values: CategoricalIndex) -> CategoricalIndex: ...
3430
@overload
3531
def unique(values: IntervalIndex[IntervalT]) -> IntervalIndex[IntervalT]: ...
3632
@overload
37-
def unique(values: Index) -> np.ndarray: ...
33+
def unique(values: PeriodIndex) -> PeriodIndex: ...
34+
@overload
35+
def unique(values: DatetimeIndex) -> np_1darray[np.datetime64] | DatetimeIndex: ...
36+
@overload
37+
def unique(values: Index) -> np_1darray[Any] | Index: ...
3838
@overload
3939
def unique(values: Categorical) -> Categorical: ...
40+
41+
# @overload
42+
# def unique(values: Series[Never]) -> np_1darray[Any] | ExtensionArray: ...
43+
# TODO: DatetimeArray python/mypy#19952
44+
# @overload
45+
# def unique(values: Series[Timestamp]) -> np_1darray[np.datetime64] | ExtensionArray: ...
46+
# @overload
47+
# def unique(values: Series[int]) -> np_1darray[np.integer] | ExtensionArray: ...
4048
@overload
41-
def unique(values: Series) -> np.ndarray | ExtensionArray: ...
49+
def unique(values: Series) -> np_1darray[Any] | ExtensionArray: ...
4250
@overload
43-
def unique(values: np.ndarray) -> np.ndarray: ...
51+
def unique(values: np_1darray[GenericT]) -> np_1darray[GenericT]: ...
4452
@overload
45-
def unique(values: ExtensionArray) -> ExtensionArray: ...
53+
def unique(values: T_EXTENSION_ARRAY) -> T_EXTENSION_ARRAY: ...
4654
@overload
4755
def factorize(
48-
values: np.ndarray,
56+
values: np_1darray[GenericT],
4957
sort: bool = ...,
5058
use_na_sentinel: bool = ...,
5159
size_hint: int | None = ...,
52-
) -> tuple[np.ndarray, np.ndarray]: ...
60+
) -> tuple[np_1darray[np.int64], np_1darray[GenericT]]: ...
5361
@overload
5462
def factorize(
5563
values: Index | Series,
5664
sort: bool = ...,
5765
use_na_sentinel: bool = ...,
5866
size_hint: int | None = ...,
59-
) -> tuple[np_1darray, Index]: ...
67+
) -> tuple[np_1darray[np.int64], Index]: ...
6068
@overload
6169
def factorize(
6270
values: Categorical,
6371
sort: bool = ...,
6472
use_na_sentinel: bool = ...,
6573
size_hint: int | None = ...,
66-
) -> tuple[np_1darray, Categorical]: ...
74+
) -> tuple[np_1darray[np.int64], Categorical]: ...
6775
def value_counts(
6876
values: AnyArrayLike | list | tuple,
6977
sort: bool = True,
@@ -73,9 +81,9 @@ def value_counts(
7381
dropna: bool = True,
7482
) -> Series: ...
7583
def take(
76-
arr: np.ndarray | ExtensionArray | Index | Series,
84+
arr: np_1darray[Any] | ExtensionArray | Index | Series,
7785
indices: TakeIndexer,
7886
axis: Literal[0, 1] = 0,
7987
allow_fill: bool = False,
8088
fill_value: Any = None,
81-
) -> np_1darray | ExtensionArray: ...
89+
) -> np_1darray[Any] | ExtensionArray: ...

pandas-stubs/core/arrays/base.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ExtensionArray:
3535
def __getitem__(self, item: ScalarIndexer) -> Any: ...
3636
@overload
3737
def __getitem__(self, item: SequenceIndexer) -> Self: ...
38-
def __setitem__(self, key: int | slice | np.ndarray, value: Any) -> None: ...
38+
def __setitem__(self, key: int | slice | npt.NDArray[Any], value: Any) -> None: ...
3939
def __len__(self) -> int: ...
4040
def __iter__(self) -> Iterator[Any]: ...
4141
def __contains__(self, item: object) -> bool | np.bool_: ...
@@ -83,7 +83,9 @@ class ExtensionArray:
8383
side: Literal["left", "right"] = ...,
8484
sorter: ListLike | None = ...,
8585
) -> np.intp: ...
86-
def factorize(self, use_na_sentinel: bool = True) -> tuple[np_1darray, Self]: ...
86+
def factorize(
87+
self, use_na_sentinel: bool = True
88+
) -> tuple[np_1darray[Any], Self]: ...
8789
def repeat(
8890
self, repeats: int | AnyArrayLikeInt | Sequence[int], axis: None = None
8991
) -> Self: ...

pandas-stubs/core/arrays/boolean.pyi

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Any
22

3-
import numpy as np
43
from pandas.core.arrays.masked import BaseMaskedArray as BaseMaskedArray
54

65
from pandas._libs.missing import NAType
7-
from pandas._typing import type_t
6+
from pandas._typing import (
7+
np_ndarray_bool,
8+
type_t,
9+
)
810

911
from pandas.core.dtypes.base import ExtensionDtype as ExtensionDtype
1012

@@ -16,7 +18,7 @@ class BooleanDtype(ExtensionDtype):
1618

1719
class BooleanArray(BaseMaskedArray):
1820
def __init__(
19-
self, values: np.ndarray, mask: np.ndarray, copy: bool = ...
21+
self, values: np_ndarray_bool, mask: np_ndarray_bool, copy: bool = ...
2022
) -> None: ...
2123
@property
2224
def dtype(self): ...

0 commit comments

Comments
 (0)