Skip to content

Commit 9a4cf3b

Browse files
jbrockmendeljreback
authored andcommitted
REF: require scalar in IntervalIndex.get_loc, get_value (#31169)
1 parent 11efca1 commit 9a4cf3b

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

pandas/core/indexes/interval.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
""" define the IntervalIndex """
22
from operator import le, lt
33
import textwrap
4-
from typing import Any, Optional, Tuple, Union
4+
from typing import TYPE_CHECKING, Any, Optional, Tuple, Union
55

66
import numpy as np
77

@@ -34,7 +34,6 @@
3434
is_object_dtype,
3535
is_scalar,
3636
)
37-
from pandas.core.dtypes.generic import ABCSeries
3837
from pandas.core.dtypes.missing import isna
3938

4039
from pandas.core import accessor
@@ -59,6 +58,10 @@
5958
from pandas.tseries.frequencies import to_offset
6059
from pandas.tseries.offsets import DateOffset
6160

61+
if TYPE_CHECKING:
62+
from pandas import Series
63+
64+
6265
_VALID_CLOSED = {"left", "right", "both", "neither"}
6366
_index_doc_kwargs = dict(ibase._index_doc_kwargs)
6467

@@ -681,7 +684,7 @@ def _searchsorted_monotonic(self, label, side, exclude_label=False):
681684
return sub_idx._searchsorted_monotonic(label, side)
682685

683686
def get_loc(
684-
self, key: Any, method: Optional[str] = None, tolerance=None
687+
self, key, method: Optional[str] = None, tolerance=None
685688
) -> Union[int, slice, np.ndarray]:
686689
"""
687690
Get integer location, slice or boolean mask for requested label.
@@ -723,10 +726,8 @@ def get_loc(
723726
"""
724727
self._check_method(method)
725728

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)
730731

731732
if isinstance(key, Interval):
732733
if self.closed != key.closed:
@@ -819,6 +820,9 @@ def get_indexer(
819820
loc = self.get_loc(key)
820821
except KeyError:
821822
loc = -1
823+
except InvalidIndexError:
824+
# i.e. non-scalar key
825+
raise TypeError(key)
822826
indexer.append(loc)
823827

824828
return ensure_platform_int(indexer)
@@ -882,25 +886,15 @@ def get_indexer_for(self, target: AnyArrayLike, **kwargs) -> np.ndarray:
882886
return self.get_indexer(target, **kwargs)
883887

884888
@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)
902891
return series.iloc[loc]
903892

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+
904898
@Appender(_index_shared_docs["where"])
905899
def where(self, cond, other=None):
906900
if other is None:

pandas/core/series.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
from pandas.core.indexes.api import (
7676
Float64Index,
7777
Index,
78+
IntervalIndex,
7879
InvalidIndexError,
7980
MultiIndex,
8081
ensure_index,
@@ -905,6 +906,9 @@ def _get_with(self, key):
905906
if key_type == "integer":
906907
if self.index.is_integer() or self.index.is_floating():
907908
return self.loc[key]
909+
elif isinstance(self.index, IntervalIndex):
910+
indexer = self.index.get_indexer_for(key)
911+
return self.iloc[indexer]
908912
else:
909913
return self._get_values(key)
910914
elif key_type == "boolean":

0 commit comments

Comments
 (0)