Skip to content

Commit 2001798

Browse files
authored
TYP: npt.NDarray (#42488)
1 parent 0e75f59 commit 2001798

File tree

12 files changed

+87
-67
lines changed

12 files changed

+87
-67
lines changed

pandas/core/algorithms.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
ArrayLike,
2828
DtypeObj,
2929
Scalar,
30+
npt,
3031
)
3132
from pandas.util._decorators import doc
3233

@@ -528,7 +529,7 @@ def factorize_array(
528529
size_hint: int | None = None,
529530
na_value=None,
530531
mask: np.ndarray | None = None,
531-
) -> tuple[np.ndarray, np.ndarray]:
532+
) -> tuple[npt.NDArray[np.intp], np.ndarray]:
532533
"""
533534
Factorize an array-like to codes and uniques.
534535

pandas/core/arrays/categorical.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
Ordered,
3838
Scalar,
3939
Shape,
40+
npt,
4041
type_t,
4142
)
4243
from pandas.compat.numpy import function as nv
@@ -2048,7 +2049,7 @@ def _validate_setitem_value(self, value):
20482049
codes = self.categories.get_indexer(rvalue)
20492050
return codes.astype(self._ndarray.dtype, copy=False)
20502051

2051-
def _reverse_indexer(self) -> dict[Hashable, np.ndarray]:
2052+
def _reverse_indexer(self) -> dict[Hashable, npt.NDArray[np.intp]]:
20522053
"""
20532054
Compute the inverse of a categorical, returning
20542055
a dict of categories -> indexers.

pandas/core/groupby/ops.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
FrameOrSeries,
3333
Shape,
3434
final,
35+
npt,
3536
)
3637
from pandas.errors import AbstractMethodError
3738
from pandas.util._decorators import cache_readonly
@@ -637,7 +638,7 @@ def __init__(
637638
sort: bool = True,
638639
group_keys: bool = True,
639640
mutated: bool = False,
640-
indexer: np.ndarray | None = None,
641+
indexer: npt.NDArray[np.intp] | None = None,
641642
dropna: bool = True,
642643
):
643644
assert isinstance(axis, Index), axis
@@ -1228,7 +1229,13 @@ def _is_indexed_like(obj, axes, axis: int) -> bool:
12281229

12291230

12301231
class DataSplitter(Generic[FrameOrSeries]):
1231-
def __init__(self, data: FrameOrSeries, labels, ngroups: int, axis: int = 0):
1232+
def __init__(
1233+
self,
1234+
data: FrameOrSeries,
1235+
labels: npt.NDArray[np.intp],
1236+
ngroups: int,
1237+
axis: int = 0,
1238+
):
12321239
self.data = data
12331240
self.labels = ensure_platform_int(labels) # _should_ already be np.intp
12341241
self.ngroups = ngroups

pandas/core/indexes/base.py

+21-26
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
Shape,
4646
T,
4747
final,
48+
npt,
4849
)
4950
from pandas.compat.numpy import function as nv
5051
from pandas.errors import (
@@ -306,8 +307,7 @@ class Index(IndexOpsMixin, PandasObject):
306307
# given the dtypes of the passed arguments
307308

308309
@final
309-
def _left_indexer_unique(self: _IndexT, other: _IndexT) -> np.ndarray:
310-
# -> np.ndarray[np.intp]
310+
def _left_indexer_unique(self: _IndexT, other: _IndexT) -> npt.NDArray[np.intp]:
311311
# Caller is responsible for ensuring other.dtype == self.dtype
312312
sv = self._get_join_target()
313313
ov = other._get_join_target()
@@ -316,7 +316,7 @@ def _left_indexer_unique(self: _IndexT, other: _IndexT) -> np.ndarray:
316316
@final
317317
def _left_indexer(
318318
self: _IndexT, other: _IndexT
319-
) -> tuple[ArrayLike, np.ndarray, np.ndarray]:
319+
) -> tuple[ArrayLike, npt.NDArray[np.intp], npt.NDArray[np.intp]]:
320320
# Caller is responsible for ensuring other.dtype == self.dtype
321321
sv = self._get_join_target()
322322
ov = other._get_join_target()
@@ -327,7 +327,7 @@ def _left_indexer(
327327
@final
328328
def _inner_indexer(
329329
self: _IndexT, other: _IndexT
330-
) -> tuple[ArrayLike, np.ndarray, np.ndarray]:
330+
) -> tuple[ArrayLike, npt.NDArray[np.intp], npt.NDArray[np.intp]]:
331331
# Caller is responsible for ensuring other.dtype == self.dtype
332332
sv = self._get_join_target()
333333
ov = other._get_join_target()
@@ -338,7 +338,7 @@ def _inner_indexer(
338338
@final
339339
def _outer_indexer(
340340
self: _IndexT, other: _IndexT
341-
) -> tuple[ArrayLike, np.ndarray, np.ndarray]:
341+
) -> tuple[ArrayLike, npt.NDArray[np.intp], npt.NDArray[np.intp]]:
342342
# Caller is responsible for ensuring other.dtype == self.dtype
343343
sv = self._get_join_target()
344344
ov = other._get_join_target()
@@ -3460,8 +3460,7 @@ def get_indexer(
34603460
method: str_t | None = None,
34613461
limit: int | None = None,
34623462
tolerance=None,
3463-
) -> np.ndarray:
3464-
# returned ndarray is np.intp
3463+
) -> npt.NDArray[np.intp]:
34653464
method = missing.clean_reindex_fill_method(method)
34663465
target = self._maybe_cast_listlike_indexer(target)
34673466

@@ -3842,7 +3841,7 @@ def _validate_can_reindex(self, indexer: np.ndarray) -> None:
38423841

38433842
def reindex(
38443843
self, target, method=None, level=None, limit=None, tolerance=None
3845-
) -> tuple[Index, np.ndarray | None]:
3844+
) -> tuple[Index, npt.NDArray[np.intp] | None]:
38463845
"""
38473846
Create index with target's values.
38483847
@@ -3918,7 +3917,7 @@ def _maybe_preserve_names(self, target: Index, preserve_names: bool):
39183917
@final
39193918
def _reindex_non_unique(
39203919
self, target: Index
3921-
) -> tuple[Index, np.ndarray, np.ndarray | None]:
3920+
) -> tuple[Index, npt.NDArray[np.intp], npt.NDArray[np.intp] | None]:
39223921
"""
39233922
Create a new index with target's values (move/add/delete values as
39243923
necessary) use with non-unique Index and a possibly non-unique target.
@@ -4206,8 +4205,7 @@ def _join_multi(self, other: Index, how: str_t):
42064205
@final
42074206
def _join_non_unique(
42084207
self, other: Index, how: str_t = "left"
4209-
) -> tuple[Index, np.ndarray, np.ndarray]:
4210-
# returned ndarrays are np.intp
4208+
) -> tuple[Index, npt.NDArray[np.intp], npt.NDArray[np.intp]]:
42114209
from pandas.core.reshape.merge import get_join_indexers
42124210

42134211
# We only get here if dtypes match
@@ -4235,8 +4233,7 @@ def _join_non_unique(
42354233
@final
42364234
def _join_level(
42374235
self, other: Index, level, how: str_t = "left", keep_order: bool = True
4238-
) -> tuple[MultiIndex, np.ndarray | None, np.ndarray | None]:
4239-
# Any returned ndarrays are np.intp
4236+
) -> tuple[MultiIndex, npt.NDArray[np.intp] | None, npt.NDArray[np.intp] | None]:
42404237
"""
42414238
The join method *only* affects the level of the resulting
42424239
MultiIndex. Otherwise it just exactly aligns the Index data to the
@@ -4248,7 +4245,7 @@ def _join_level(
42484245
"""
42494246
from pandas.core.indexes.multi import MultiIndex
42504247

4251-
def _get_leaf_sorter(labels: list[np.ndarray]) -> np.ndarray:
4248+
def _get_leaf_sorter(labels: list[np.ndarray]) -> npt.NDArray[np.intp]:
42524249
"""
42534250
Returns sorter for the inner most level while preserving the
42544251
order of higher levels.
@@ -5000,7 +4997,7 @@ def asof(self, label):
50004997

50014998
return self[loc]
50024999

5003-
def asof_locs(self, where: Index, mask: np.ndarray) -> np.ndarray:
5000+
def asof_locs(self, where: Index, mask: np.ndarray) -> npt.NDArray[np.intp]:
50045001
"""
50055002
Return the locations (indices) of labels in the index.
50065003
@@ -5191,7 +5188,7 @@ def shift(self, periods=1, freq=None):
51915188
f"TimedeltaIndex; Got type {type(self).__name__}"
51925189
)
51935190

5194-
def argsort(self, *args, **kwargs) -> np.ndarray:
5191+
def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
51955192
"""
51965193
Return the integer indices that would sort the index.
51975194
@@ -5342,8 +5339,9 @@ def set_value(self, arr, key, value):
53425339
"""
53435340

53445341
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
5345-
def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
5346-
# both returned ndarrays are np.intp
5342+
def get_indexer_non_unique(
5343+
self, target
5344+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
53475345
target = ensure_index(target)
53485346
target = self._maybe_cast_listlike_indexer(target)
53495347

@@ -5373,7 +5371,7 @@ def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
53735371
return ensure_platform_int(indexer), ensure_platform_int(missing)
53745372

53755373
@final
5376-
def get_indexer_for(self, target) -> np.ndarray:
5374+
def get_indexer_for(self, target) -> npt.NDArray[np.intp]:
53775375
"""
53785376
Guaranteed return of an indexer even when non-unique.
53795377
@@ -5393,28 +5391,25 @@ def get_indexer_for(self, target) -> np.ndarray:
53935391
@overload
53945392
def _get_indexer_non_comparable(
53955393
self, target: Index, method, unique: Literal[True] = ...
5396-
) -> np.ndarray:
5397-
# returned ndarray is np.intp
5394+
) -> npt.NDArray[np.intp]:
53985395
...
53995396

54005397
@overload
54015398
def _get_indexer_non_comparable(
54025399
self, target: Index, method, unique: Literal[False]
5403-
) -> tuple[np.ndarray, np.ndarray]:
5404-
# both returned ndarrays are np.intp
5400+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
54055401
...
54065402

54075403
@overload
54085404
def _get_indexer_non_comparable(
54095405
self, target: Index, method, unique: bool = True
5410-
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
5411-
# any returned ndarrays are np.intp
5406+
) -> npt.NDArray[np.intp] | tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
54125407
...
54135408

54145409
@final
54155410
def _get_indexer_non_comparable(
54165411
self, target: Index, method, unique: bool = True
5417-
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
5412+
) -> npt.NDArray[np.intp] | tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
54185413
"""
54195414
Called from get_indexer or get_indexer_non_unique when the target
54205415
is of a non-comparable dtype.

pandas/core/indexes/category.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pandas._typing import (
1515
Dtype,
1616
DtypeObj,
17+
npt,
1718
)
1819
from pandas.util._decorators import doc
1920

@@ -368,7 +369,7 @@ def fillna(self, value, downcast=None):
368369

369370
def reindex(
370371
self, target, method=None, level=None, limit=None, tolerance=None
371-
) -> tuple[Index, np.ndarray | None]:
372+
) -> tuple[Index, npt.NDArray[np.intp] | None]:
372373
"""
373374
Create index with target's values (move/add/delete values as necessary)
374375

pandas/core/indexes/datetimes.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from pandas._typing import (
3434
Dtype,
3535
DtypeObj,
36+
npt,
3637
)
3738
from pandas.util._decorators import (
3839
cache_readonly,
@@ -807,7 +808,7 @@ def inferred_type(self) -> str:
807808
# sure we can't have ambiguous indexing
808809
return "datetime64"
809810

810-
def indexer_at_time(self, time, asof: bool = False) -> np.ndarray:
811+
def indexer_at_time(self, time, asof: bool = False) -> npt.NDArray[np.intp]:
811812
"""
812813
Return index locations of values at particular time of day
813814
(e.g. 9:30AM).
@@ -848,7 +849,7 @@ def indexer_at_time(self, time, asof: bool = False) -> np.ndarray:
848849

849850
def indexer_between_time(
850851
self, start_time, end_time, include_start: bool = True, include_end: bool = True
851-
) -> np.ndarray:
852+
) -> npt.NDArray[np.intp]:
852853
"""
853854
Return index locations of values between particular times of day
854855
(e.g., 9:00-9:30AM).

pandas/core/indexes/interval.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from pandas._typing import (
2929
Dtype,
3030
DtypeObj,
31+
npt,
3132
)
3233
from pandas.errors import InvalidIndexError
3334
from pandas.util._decorators import (
@@ -644,8 +645,7 @@ def _get_indexer(
644645
method: str | None = None,
645646
limit: int | None = None,
646647
tolerance: Any | None = None,
647-
) -> np.ndarray:
648-
# returned ndarray is np.intp
648+
) -> npt.NDArray[np.intp]:
649649

650650
if isinstance(target, IntervalIndex):
651651
# non-overlapping -> at most one match per interval in target
@@ -668,8 +668,9 @@ def _get_indexer(
668668
return ensure_platform_int(indexer)
669669

670670
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
671-
def get_indexer_non_unique(self, target: Index) -> tuple[np.ndarray, np.ndarray]:
672-
# both returned ndarrays are np.intp
671+
def get_indexer_non_unique(
672+
self, target: Index
673+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
673674
target = ensure_index(target)
674675

675676
if not self._should_compare(target) and not self._should_partial_index(target):
@@ -689,8 +690,9 @@ def get_indexer_non_unique(self, target: Index) -> tuple[np.ndarray, np.ndarray]
689690

690691
return ensure_platform_int(indexer), ensure_platform_int(missing)
691692

692-
def _get_indexer_pointwise(self, target: Index) -> tuple[np.ndarray, np.ndarray]:
693-
# both returned ndarrays are np.intp
693+
def _get_indexer_pointwise(
694+
self, target: Index
695+
) -> tuple[npt.NDArray[np.intp], npt.NDArray[np.intp]]:
694696
"""
695697
pointwise implementation for get_indexer and get_indexer_non_unique.
696698
"""

pandas/core/indexes/range.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
from pandas._libs import index as libindex
1919
from pandas._libs.lib import no_default
20-
from pandas._typing import Dtype
20+
from pandas._typing import (
21+
Dtype,
22+
npt,
23+
)
2124
from pandas.compat.numpy import function as nv
2225
from pandas.util._decorators import (
2326
cache_readonly,
@@ -395,8 +398,7 @@ def _get_indexer(
395398
method: str | None = None,
396399
limit: int | None = None,
397400
tolerance=None,
398-
) -> np.ndarray:
399-
# -> np.ndarray[np.intp]
401+
) -> npt.NDArray[np.intp]:
400402
if com.any_not_none(method, tolerance, limit):
401403
return super()._get_indexer(
402404
target, method=method, tolerance=tolerance, limit=limit
@@ -502,7 +504,7 @@ def max(self, axis=None, skipna: bool = True, *args, **kwargs) -> int:
502504
nv.validate_max(args, kwargs)
503505
return self._minmax("max")
504506

505-
def argsort(self, *args, **kwargs) -> np.ndarray:
507+
def argsort(self, *args, **kwargs) -> npt.NDArray[np.intp]:
506508
"""
507509
Returns the indices that would sort the index and its
508510
underlying data.
@@ -529,7 +531,7 @@ def argsort(self, *args, **kwargs) -> np.ndarray:
529531

530532
def factorize(
531533
self, sort: bool = False, na_sentinel: int | None = -1
532-
) -> tuple[np.ndarray, RangeIndex]:
534+
) -> tuple[npt.NDArray[np.intp], RangeIndex]:
533535
codes = np.arange(len(self), dtype=np.intp)
534536
uniques = self
535537
if sort and self.step < 0:

pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
TimedeltaConvertibleTypes,
2929
TimestampConvertibleTypes,
3030
final,
31+
npt,
3132
)
3233
from pandas.compat.numpy import function as nv
3334
from pandas.errors import AbstractMethodError
@@ -1768,9 +1769,8 @@ def _get_period_bins(self, ax: PeriodIndex):
17681769

17691770

17701771
def _take_new_index(
1771-
obj: FrameOrSeries, indexer: np.ndarray, new_index: Index, axis: int = 0
1772+
obj: FrameOrSeries, indexer: npt.NDArray[np.intp], new_index: Index, axis: int = 0
17721773
) -> FrameOrSeries:
1773-
# indexer: np.ndarray[np.intp]
17741774

17751775
if isinstance(obj, ABCSeries):
17761776
new_values = algos.take_nd(obj._values, indexer)

0 commit comments

Comments
 (0)