Skip to content

Commit 463cd0a

Browse files
authored
REF: de-duplicate pointwise get_indexer for IntervalIndex (#37919)
1 parent df32e83 commit 463cd0a

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

pandas/core/indexes/interval.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -732,17 +732,7 @@ def get_indexer(
732732
indexer = self._engine.get_indexer(target_as_index.values)
733733
else:
734734
# heterogeneous scalar index: defer elementwise to get_loc
735-
# (non-overlapping so get_loc guarantees scalar of KeyError)
736-
indexer = []
737-
for key in target_as_index:
738-
try:
739-
loc = self.get_loc(key)
740-
except KeyError:
741-
loc = -1
742-
except InvalidIndexError as err:
743-
# i.e. non-scalar key
744-
raise TypeError(key) from err
745-
indexer.append(loc)
735+
return self._get_indexer_pointwise(target_as_index)[0]
746736

747737
return ensure_platform_int(indexer)
748738

@@ -766,18 +756,8 @@ def get_indexer_non_unique(
766756
target_as_index, IntervalIndex
767757
):
768758
# target_as_index might contain intervals: defer elementwise to get_loc
769-
indexer, missing = [], []
770-
for i, key in enumerate(target_as_index):
771-
try:
772-
locs = self.get_loc(key)
773-
if isinstance(locs, slice):
774-
locs = np.arange(locs.start, locs.stop, locs.step, dtype="intp")
775-
locs = np.array(locs, ndmin=1)
776-
except KeyError:
777-
missing.append(i)
778-
locs = np.array([-1])
779-
indexer.append(locs)
780-
indexer = np.concatenate(indexer)
759+
return self._get_indexer_pointwise(target_as_index)
760+
781761
else:
782762
target_as_index = self._maybe_convert_i8(target_as_index)
783763
indexer, missing = self._engine.get_indexer_non_unique(
@@ -786,6 +766,30 @@ def get_indexer_non_unique(
786766

787767
return ensure_platform_int(indexer), ensure_platform_int(missing)
788768

769+
def _get_indexer_pointwise(self, target: Index) -> Tuple[np.ndarray, np.ndarray]:
770+
"""
771+
pointwise implementation for get_indexer and get_indexer_non_unique.
772+
"""
773+
indexer, missing = [], []
774+
for i, key in enumerate(target):
775+
try:
776+
locs = self.get_loc(key)
777+
if isinstance(locs, slice):
778+
# Only needed for get_indexer_non_unique
779+
locs = np.arange(locs.start, locs.stop, locs.step, dtype="intp")
780+
locs = np.array(locs, ndmin=1)
781+
except KeyError:
782+
missing.append(i)
783+
locs = np.array([-1])
784+
except InvalidIndexError as err:
785+
# i.e. non-scalar key
786+
raise TypeError(key) from err
787+
788+
indexer.append(locs)
789+
790+
indexer = np.concatenate(indexer)
791+
return ensure_platform_int(indexer), ensure_platform_int(missing)
792+
789793
@property
790794
def _index_as_unique(self):
791795
return not self.is_overlapping

0 commit comments

Comments
 (0)