|
1 | 1 | """ define the IntervalIndex """
|
2 | 2 | from operator import le, lt
|
3 | 3 | import textwrap
|
4 |
| -from typing import Any, Optional, Tuple, Union |
| 4 | +from typing import TYPE_CHECKING, Any, Optional, Tuple, Union |
5 | 5 |
|
6 | 6 | import numpy as np
|
7 | 7 |
|
|
34 | 34 | is_object_dtype,
|
35 | 35 | is_scalar,
|
36 | 36 | )
|
37 |
| -from pandas.core.dtypes.generic import ABCSeries |
38 | 37 | from pandas.core.dtypes.missing import isna
|
39 | 38 |
|
40 | 39 | from pandas.core import accessor
|
|
59 | 58 | from pandas.tseries.frequencies import to_offset
|
60 | 59 | from pandas.tseries.offsets import DateOffset
|
61 | 60 |
|
| 61 | +if TYPE_CHECKING: |
| 62 | + from pandas import Series |
| 63 | + |
| 64 | + |
62 | 65 | _VALID_CLOSED = {"left", "right", "both", "neither"}
|
63 | 66 | _index_doc_kwargs = dict(ibase._index_doc_kwargs)
|
64 | 67 |
|
@@ -681,7 +684,7 @@ def _searchsorted_monotonic(self, label, side, exclude_label=False):
|
681 | 684 | return sub_idx._searchsorted_monotonic(label, side)
|
682 | 685 |
|
683 | 686 | def get_loc(
|
684 |
| - self, key: Any, method: Optional[str] = None, tolerance=None |
| 687 | + self, key, method: Optional[str] = None, tolerance=None |
685 | 688 | ) -> Union[int, slice, np.ndarray]:
|
686 | 689 | """
|
687 | 690 | Get integer location, slice or boolean mask for requested label.
|
@@ -723,10 +726,8 @@ def get_loc(
|
723 | 726 | """
|
724 | 727 | self._check_method(method)
|
725 | 728 |
|
726 |
| - # list-like are invalid labels for II but in some cases may work, e.g |
727 |
| - # single element array of comparable type, so guard against them early |
728 |
| - if is_list_like(key): |
729 |
| - raise KeyError(key) |
| 729 | + if not is_scalar(key): |
| 730 | + raise InvalidIndexError(key) |
730 | 731 |
|
731 | 732 | if isinstance(key, Interval):
|
732 | 733 | if self.closed != key.closed:
|
@@ -819,6 +820,9 @@ def get_indexer(
|
819 | 820 | loc = self.get_loc(key)
|
820 | 821 | except KeyError:
|
821 | 822 | loc = -1
|
| 823 | + except InvalidIndexError: |
| 824 | + # i.e. non-scalar key |
| 825 | + raise TypeError(key) |
822 | 826 | indexer.append(loc)
|
823 | 827 |
|
824 | 828 | return ensure_platform_int(indexer)
|
@@ -882,25 +886,15 @@ def get_indexer_for(self, target: AnyArrayLike, **kwargs) -> np.ndarray:
|
882 | 886 | return self.get_indexer(target, **kwargs)
|
883 | 887 |
|
884 | 888 | @Appender(_index_shared_docs["get_value"] % _index_doc_kwargs)
|
885 |
| - def get_value(self, series: ABCSeries, key: Any) -> Any: |
886 |
| - |
887 |
| - if com.is_bool_indexer(key): |
888 |
| - loc = key |
889 |
| - elif is_list_like(key): |
890 |
| - if self.is_overlapping: |
891 |
| - loc, missing = self.get_indexer_non_unique(key) |
892 |
| - if len(missing): |
893 |
| - raise KeyError |
894 |
| - else: |
895 |
| - loc = self.get_indexer(key) |
896 |
| - elif isinstance(key, slice): |
897 |
| - if not (key.step is None or key.step == 1): |
898 |
| - raise ValueError("cannot support not-default step in a slice") |
899 |
| - loc = self._convert_slice_indexer(key, kind="getitem") |
900 |
| - else: |
901 |
| - loc = self.get_loc(key) |
| 889 | + def get_value(self, series: "Series", key): |
| 890 | + loc = self.get_loc(key) |
902 | 891 | return series.iloc[loc]
|
903 | 892 |
|
| 893 | + def _convert_slice_indexer(self, key: slice, kind=None): |
| 894 | + if not (key.step is None or key.step == 1): |
| 895 | + raise ValueError("cannot support not-default step in a slice") |
| 896 | + return super()._convert_slice_indexer(key, kind) |
| 897 | + |
904 | 898 | @Appender(_index_shared_docs["where"])
|
905 | 899 | def where(self, cond, other=None):
|
906 | 900 | if other is None:
|
|
0 commit comments