Skip to content

Commit

Permalink
Add type annotation to the return value of Index#get_loc (#863)
Browse files Browse the repository at this point in the history
Add type annotation to the return value of Index#get_loc
  • Loading branch information
skatsuta authored Feb 12, 2024
1 parent 6fd6145 commit fe28163
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas-stubs/core/indexes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class Index(IndexOpsMixin[S1]):
key: Label,
method: FillnaOptions | Literal["nearest"] | None = ...,
tolerance=...,
): ...
) -> int | slice | np_ndarray_bool: ...
def get_indexer(self, target, method=..., limit=..., tolerance=...): ...
def reindex(self, target, method=..., level=..., limit=..., tolerance=...): ...
def join(
Expand Down
27 changes: 27 additions & 0 deletions tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1060,3 +1060,30 @@ def test_datetime_operators_builtin() -> None:
check(assert_type(delta + dt.timedelta(0), pd.TimedeltaIndex), pd.TimedeltaIndex)
check(assert_type(dt.datetime.now() + delta, pd.DatetimeIndex), pd.DatetimeIndex)
check(assert_type(delta - dt.timedelta(0), pd.TimedeltaIndex), pd.TimedeltaIndex)


def test_get_loc() -> None:
unique_index = pd.Index(list("abc"))
check(
assert_type(
unique_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
),
int,
)

monotonic_index = pd.Index(list("abbc"))
check(
assert_type(
monotonic_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
),
slice,
)

non_monotonic_index = pd.Index(list("abcb"))
check(
assert_type(
non_monotonic_index.get_loc("b"), Union[int, slice, npt.NDArray[np.bool_]]
),
np.ndarray,
np.bool_,
)

0 comments on commit fe28163

Please sign in to comment.