Skip to content

Add type annotation to the return value of Index#get_loc #863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_,
)