add IntervalIndex.get_loc_exact · pandas-dev/pandas@7590b87 (original) (raw)
`@@ -903,11 +903,8 @@ def _get_loc_only_exact_matches(self, key):
`
903
903
`if not self.is_unique:
`
904
904
`raise ValueError("cannot index with a slice Interval"
`
905
905
`" and a non-unique index")
`
906
``
-
907
``
`-
TODO: this expands to a tuple index, see if we can
`
908
``
`-
do better
`
909
``
`-
return Index(self._multiindex.values).get_loc(key)
`
910
``
`-
raise KeyError
`
``
906
`+
return super(IntervalIndex, self)._engine.get_loc(key)
`
``
907
`+
raise KeyError(key)
`
911
908
``
912
909
`def _find_non_overlapping_monotonic_bounds(self, key):
`
913
910
`if isinstance(key, IntervalMixin):
`
`@@ -970,6 +967,10 @@ def get_loc(self, key, method=None):
`
970
967
` >>> overlapping_index = pd.IntervalIndex([i2, i3])
`
971
968
` >>> overlapping_index.get_loc(1.5)
`
972
969
` array([0, 1], dtype=int64)
`
``
970
+
``
971
`+
See Also
`
``
972
`+
`
``
973
`+
get_loc_exact : Exact matches only
`
973
974
` """
`
974
975
`self._check_method(method)
`
975
976
``
`@@ -1003,6 +1004,47 @@ def get_loc(self, key, method=None):
`
1003
1004
`else:
`
1004
1005
`return self._engine.get_loc(key)
`
1005
1006
``
``
1007
`+
def get_loc_exact(self, key, method=None):
`
``
1008
`+
"""Get integer location, slice or boolean mask for exact
`
``
1009
`+
Interval matches only.
`
``
1010
+
``
1011
`+
Parameters
`
``
1012
`+
`
``
1013
`+
key : Interval
`
``
1014
`+
The label we want to find locations for. Must have type
`
``
1015
`` +
:class:Interval
``
``
1016
`+
method : {None}, optional
`
``
1017
`+
- default: matches where the label exactly matches a given
`
``
1018
`` +
:class:Interval
.
``
``
1019
+
``
1020
`+
Returns
`
``
1021
`+
`
``
1022
`+
loc : int if unique index, slice if monotonic index, else mask
`
``
1023
+
``
1024
`+
Examples
`
``
1025
`+
`
``
1026
`+
i1, i2 = pd.Interval(0, 1), pd.Interval(1, 2)
`
``
1027
`+
index = pd.IntervalIndex([i1, i2])
`
``
1028
`+
index.get_loc_exact(i1)
`
``
1029
`+
0
`
``
1030
+
``
1031
`+
If an exact match is not found, a KeyError is raised
`
``
1032
`+
index.get_loc_exact(pd.Interval(0.5, 1.5))
`
``
1033
`+
KeyError: Interval(0.5, 1.5, closed='right')
`
``
1034
+
``
1035
`+
If a label is in several locations, you get all the relevant
`
``
1036
`+
locations.
`
``
1037
+
``
1038
`+
index = pd.IntervalIndex([i1, i2, i1])
`
``
1039
`+
index.get_loc_exact(i1)
`
``
1040
`+
array([ True, False, True], dtype=bool)
`
``
1041
+
``
1042
`+
See Also
`
``
1043
`+
`
``
1044
`+
get_loc
`
``
1045
`+
"""
`
``
1046
`+
return self.astype(object).get_loc(key, method=method)
`
``
1047
+
1006
1048
`def get_value(self, series, key):
`
1007
1049
`if com.is_bool_indexer(key):
`
1008
1050
`loc = key
`