Skip to content

Commit fee34e8

Browse files
committed
Add type annotation to the return value of Index#get_loc
1 parent e35c3ca commit fee34e8

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,3 +1060,25 @@ 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(unique_index.get_loc("b"), int | slice | npt.NDArray[np.bool_]), int
1069+
)
1070+
1071+
monotonic_index = pd.Index(list("abbc"))
1072+
check(
1073+
assert_type(monotonic_index.get_loc("b"), int | slice | npt.NDArray[np.bool_]),
1074+
slice,
1075+
)
1076+
1077+
non_monotonic_index = pd.Index(list("abcb"))
1078+
check(
1079+
assert_type(
1080+
non_monotonic_index.get_loc("b"), int | slice | npt.NDArray[np.bool_]
1081+
),
1082+
np.ndarray,
1083+
np.bool_,
1084+
)

0 commit comments

Comments
 (0)