Skip to content

Commit 8fa4f11

Browse files
jbrockmendelyeshsurya
authored andcommitted
TYP: get_indexer (pandas-dev#40612)
* TYP: get_indexer * update per discussion in pandas-dev#40612 * one more overload * pre-commit fixup
1 parent 331fef4 commit 8fa4f11

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

pandas/core/indexes/base.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Sequence,
1414
TypeVar,
1515
cast,
16+
overload,
1617
)
1718
import warnings
1819

@@ -159,6 +160,8 @@
159160
)
160161

161162
if TYPE_CHECKING:
163+
from typing import Literal
164+
162165
from pandas import (
163166
CategoricalIndex,
164167
DataFrame,
@@ -5212,7 +5215,8 @@ def set_value(self, arr, key, value):
52125215
"""
52135216

52145217
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
5215-
def get_indexer_non_unique(self, target):
5218+
def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
5219+
# both returned ndarrays are np.intp
52165220
target = ensure_index(target)
52175221

52185222
if not self._should_compare(target) and not is_interval_dtype(self.dtype):
@@ -5236,7 +5240,7 @@ def get_indexer_non_unique(self, target):
52365240
tgt_values = target._get_engine_target()
52375241

52385242
indexer, missing = self._engine.get_indexer_non_unique(tgt_values)
5239-
return ensure_platform_int(indexer), missing
5243+
return ensure_platform_int(indexer), ensure_platform_int(missing)
52405244

52415245
@final
52425246
def get_indexer_for(self, target, **kwargs) -> np.ndarray:
@@ -5256,8 +5260,31 @@ def get_indexer_for(self, target, **kwargs) -> np.ndarray:
52565260
indexer, _ = self.get_indexer_non_unique(target)
52575261
return indexer
52585262

5263+
@overload
5264+
def _get_indexer_non_comparable(
5265+
self, target: Index, method, unique: Literal[True] = ...
5266+
) -> np.ndarray:
5267+
# returned ndarray is np.intp
5268+
...
5269+
5270+
@overload
5271+
def _get_indexer_non_comparable(
5272+
self, target: Index, method, unique: Literal[False]
5273+
) -> tuple[np.ndarray, np.ndarray]:
5274+
# both returned ndarrays are np.intp
5275+
...
5276+
5277+
@overload
5278+
def _get_indexer_non_comparable(
5279+
self, target: Index, method, unique: bool = True
5280+
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
5281+
# any returned ndarrays are np.intp
5282+
...
5283+
52595284
@final
5260-
def _get_indexer_non_comparable(self, target: Index, method, unique: bool = True):
5285+
def _get_indexer_non_comparable(
5286+
self, target: Index, method, unique: bool = True
5287+
) -> np.ndarray | tuple[np.ndarray, np.ndarray]:
52615288
"""
52625289
Called from get_indexer or get_indexer_non_unique when the target
52635290
is of a non-comparable dtype.

pandas/core/indexes/category.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -482,18 +482,23 @@ def _get_indexer(
482482
limit: int | None = None,
483483
tolerance=None,
484484
) -> np.ndarray:
485+
# returned ndarray is np.intp
485486

486487
if self.equals(target):
487488
return np.arange(len(self), dtype="intp")
488489

489490
return self._get_indexer_non_unique(target._values)[0]
490491

491492
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
492-
def get_indexer_non_unique(self, target):
493+
def get_indexer_non_unique(self, target) -> tuple[np.ndarray, np.ndarray]:
494+
# both returned ndarrays are np.intp
493495
target = ibase.ensure_index(target)
494496
return self._get_indexer_non_unique(target._values)
495497

496-
def _get_indexer_non_unique(self, values: ArrayLike):
498+
def _get_indexer_non_unique(
499+
self, values: ArrayLike
500+
) -> tuple[np.ndarray, np.ndarray]:
501+
# both returned ndarrays are np.intp
497502
"""
498503
get_indexer_non_unique but after unrapping the target Index object.
499504
"""
@@ -512,7 +517,7 @@ def _get_indexer_non_unique(self, values: ArrayLike):
512517
codes = self.categories.get_indexer(values)
513518

514519
indexer, missing = self._engine.get_indexer_non_unique(codes)
515-
return ensure_platform_int(indexer), missing
520+
return ensure_platform_int(indexer), ensure_platform_int(missing)
516521

517522
@doc(Index._convert_list_indexer)
518523
def _convert_list_indexer(self, keyarr):

pandas/core/indexes/interval.py

+3
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,7 @@ def _get_indexer(
721721
limit: int | None = None,
722722
tolerance: Any | None = None,
723723
) -> np.ndarray:
724+
# returned ndarray is np.intp
724725

725726
if isinstance(target, IntervalIndex):
726727
# equal indexes -> 1:1 positional match
@@ -753,6 +754,7 @@ def _get_indexer(
753754

754755
@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
755756
def get_indexer_non_unique(self, target: Index) -> tuple[np.ndarray, np.ndarray]:
757+
# both returned ndarrays are np.intp
756758
target = ensure_index(target)
757759

758760
if isinstance(target, IntervalIndex) and not self._should_compare(target):
@@ -772,6 +774,7 @@ def get_indexer_non_unique(self, target: Index) -> tuple[np.ndarray, np.ndarray]
772774
return ensure_platform_int(indexer), ensure_platform_int(missing)
773775

774776
def _get_indexer_pointwise(self, target: Index) -> tuple[np.ndarray, np.ndarray]:
777+
# both returned ndarrays are np.intp
775778
"""
776779
pointwise implementation for get_indexer and get_indexer_non_unique.
777780
"""

0 commit comments

Comments
 (0)