Skip to content

Commit 7ba3ea5

Browse files
committed
reduce ndarray
1 parent 84f8d36 commit 7ba3ea5

33 files changed

+381
-278
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ ToTimestampHow: TypeAlias = Literal["s", "e", "start", "end"]
166166
NDFrameT = TypeVar("NDFrameT", bound=NDFrame)
167167

168168
IndexT = TypeVar("IndexT", bound=Index)
169+
T_EXTENSION_ARRAY = TypeVar("T_EXTENSION_ARRAY", bound=ExtensionArray)
169170

170171
# From _typing.py, not used here:
171172
# FreqIndexT = TypeVar("FreqIndexT", "DatetimeIndex", "PeriodIndex", "TimedeltaIndex")

pandas-stubs/core/algorithms.pyi

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,65 +5,75 @@ 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-
)
8+
from numpy import typing as npt
169
from pandas.api.extensions import ExtensionArray
10+
from pandas.core.arrays.categorical import Categorical
11+
from pandas.core.indexes.base import Index
12+
from pandas.core.indexes.category import CategoricalIndex
13+
from pandas.core.indexes.datetimes import DatetimeIndex
14+
from pandas.core.indexes.interval import IntervalIndex
15+
from pandas.core.indexes.period import PeriodIndex
16+
from pandas.core.series import Series
1717

1818
from pandas._typing import (
19+
T_EXTENSION_ARRAY,
1920
AnyArrayLike,
21+
GenericT,
2022
IntervalT,
2123
TakeIndexer,
2224
np_1darray,
25+
np_ndarray,
2326
)
2427

2528
# These are type: ignored because the Index types overlap due to inheritance but indices
2629
# 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: ...
3230
@overload
33-
def unique(values: CategoricalIndex) -> CategoricalIndex: ... # type: ignore[overload-overlap]
31+
def unique(values: CategoricalIndex) -> CategoricalIndex: ...
3432
@overload
3533
def unique(values: IntervalIndex[IntervalT]) -> IntervalIndex[IntervalT]: ...
3634
@overload
37-
def unique(values: Index) -> np.ndarray: ...
35+
def unique(values: PeriodIndex) -> PeriodIndex: ...
36+
@overload
37+
def unique(values: DatetimeIndex) -> np_1darray[np.datetime64] | DatetimeIndex: ...
38+
@overload
39+
def unique(values: Index) -> np_1darray[Any] | Index: ...
3840
@overload
3941
def unique(values: Categorical) -> Categorical: ...
42+
43+
# @overload
44+
# def unique(values: Series[Never]) -> np_1darray[Any] | ExtensionArray: ...
45+
# TODO: DatetimeArray python/mypy#19952
46+
# @overload
47+
# def unique(values: Series[Timestamp]) -> np_1darray[np.datetime64] | ExtensionArray: ...
48+
# @overload
49+
# def unique(values: Series[int]) -> np_1darray[np.integer] | ExtensionArray: ...
4050
@overload
41-
def unique(values: Series) -> np.ndarray | ExtensionArray: ...
51+
def unique(values: Series) -> np_1darray[Any] | ExtensionArray: ...
4252
@overload
43-
def unique(values: np.ndarray) -> np.ndarray: ...
53+
def unique(values: npt.NDArray[Any]) -> np_1darray[Any]: ...
4454
@overload
45-
def unique(values: ExtensionArray) -> ExtensionArray: ...
55+
def unique(values: T_EXTENSION_ARRAY) -> T_EXTENSION_ARRAY: ...
4656
@overload
4757
def factorize(
48-
values: np.ndarray,
58+
values: np_ndarray[Any, GenericT],
4959
sort: bool = ...,
5060
use_na_sentinel: bool = ...,
5161
size_hint: int | None = ...,
52-
) -> tuple[np.ndarray, np.ndarray]: ...
62+
) -> tuple[np_1darray[np.int64], np_1darray[GenericT]]: ...
5363
@overload
5464
def factorize(
5565
values: Index | Series,
5666
sort: bool = ...,
5767
use_na_sentinel: bool = ...,
5868
size_hint: int | None = ...,
59-
) -> tuple[np_1darray, Index]: ...
69+
) -> tuple[np_1darray[np.int64], Index]: ...
6070
@overload
6171
def factorize(
6272
values: Categorical,
6373
sort: bool = ...,
6474
use_na_sentinel: bool = ...,
6575
size_hint: int | None = ...,
66-
) -> tuple[np_1darray, Categorical]: ...
76+
) -> tuple[np_1darray[np.int64], Categorical]: ...
6777
def value_counts(
6878
values: AnyArrayLike | list | tuple,
6979
sort: bool = True,
@@ -73,9 +83,9 @@ def value_counts(
7383
dropna: bool = True,
7484
) -> Series: ...
7585
def take(
76-
arr: np.ndarray | ExtensionArray | Index | Series,
86+
arr: npt.NDArray[Any] | ExtensionArray | Index | Series,
7787
indices: TakeIndexer,
7888
axis: Literal[0, 1] = 0,
7989
allow_fill: bool = False,
8090
fill_value: Any = None,
81-
) -> np_1darray | ExtensionArray: ...
91+
) -> 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): ...

pandas-stubs/core/arrays/datetimelike.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ from typing import (
55
overload,
66
)
77

8-
import numpy as np
98
from pandas.core.arrays.base import (
109
ExtensionArray,
1110
ExtensionOpsMixin,
@@ -65,7 +64,7 @@ class DatetimeLikeArrayMixin(ExtensionOpsMixin, ExtensionArray):
6564
def ravel(self, *args: Any, **kwargs: Any): ...
6665
def __iter__(self): ...
6766
@property
68-
def asi8(self) -> np.ndarray: ...
67+
def asi8(self) -> np_1darray[Any]: ...
6968
@property
7069
def nbytes(self): ...
7170
def __array__(

pandas-stubs/core/arrays/interval.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from typing import (
55
)
66

77
import numpy as np
8+
from numpy import typing as npt
89
from pandas.core.arrays.base import ExtensionArray as ExtensionArray
910
from pandas.core.indexes.base import Index
1011
from pandas.core.series import Series
@@ -105,6 +106,6 @@ class IntervalArray(IntervalMixin, ExtensionArray):
105106
def contains(self, other: Series) -> Series[bool]: ...
106107
@overload
107108
def contains(
108-
self, other: Scalar | ExtensionArray | Index | np.ndarray
109+
self, other: Scalar | ExtensionArray | Index | npt.NDArray[Any]
109110
) -> np_1darray[np.bool]: ...
110111
def overlaps(self, other: Interval) -> bool: ...

pandas-stubs/core/arrays/masked.pyi

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ from typing import (
33
overload,
44
)
55

6-
import numpy as np
76
from pandas.core.arrays import (
87
ExtensionArray as ExtensionArray,
98
ExtensionOpsMixin,
@@ -31,7 +30,7 @@ class BaseMaskedArray(ExtensionArray, ExtensionOpsMixin):
3130
dtype: npt.DTypeLike | None = ...,
3231
copy: bool = False,
3332
na_value: Scalar = ...,
34-
) -> np.ndarray: ...
33+
) -> np_1darray[Any]: ...
3534
__array_priority__: int = ...
3635
def __array__(
3736
self, dtype: NpDtype | None = None, copy: bool | None = None

0 commit comments

Comments
 (0)