Skip to content

Commit fe28163

Browse files
authored
Add type annotation to the return value of Index#get_loc (#863)
Add type annotation to the return value of Index#get_loc
1 parent 6fd6145 commit fe28163

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

pandas-stubs/core/indexes/base.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class Index(IndexOpsMixin[S1]):
353353
key: Label,
354354
method: FillnaOptions | Literal["nearest"] | None = ...,
355355
tolerance=...,
356-
): ...
356+
) -> int | slice | np_ndarray_bool: ...
357357
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
358358
def reindex(self, target, method=..., level=..., limit=..., tolerance=...): ...
359359
def join(

tests/test_indexes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,30 @@ def test_datetime_operators_builtin() -> None:
10601060
check(assert_type(delta + dt.timedelta(0), pd.TimedeltaIndex), pd.TimedeltaIndex)
10611061
check(assert_type(dt.datetime.now() + delta, pd.DatetimeIndex), pd.DatetimeIndex)
10621062
check(assert_type(delta - dt.timedelta(0), pd.TimedeltaIndex), pd.TimedeltaIndex)
1063+
1064+
1065+
def test_get_loc() -> None:
1066+
unique_index = pd.Index(list("abc"))
1067+
check(
1068+
assert_type(
1069+
unique_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
1070+
),
1071+
int,
1072+
)
1073+
1074+
monotonic_index = pd.Index(list("abbc"))
1075+
check(
1076+
assert_type(
1077+
monotonic_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
1078+
),
1079+
slice,
1080+
)
1081+
1082+
non_monotonic_index = pd.Index(list("abcb"))
1083+
check(
1084+
assert_type(
1085+
non_monotonic_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
1086+
),
1087+
np.ndarray,
1088+
np.bool_,
1089+
)

0 commit comments

Comments
 (0)