@@ -732,17 +732,7 @@ def get_indexer(
732
732
indexer = self ._engine .get_indexer (target_as_index .values )
733
733
else :
734
734
# 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 ]
746
736
747
737
return ensure_platform_int (indexer )
748
738
@@ -766,18 +756,8 @@ def get_indexer_non_unique(
766
756
target_as_index , IntervalIndex
767
757
):
768
758
# 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
+
781
761
else :
782
762
target_as_index = self ._maybe_convert_i8 (target_as_index )
783
763
indexer , missing = self ._engine .get_indexer_non_unique (
@@ -786,6 +766,30 @@ def get_indexer_non_unique(
786
766
787
767
return ensure_platform_int (indexer ), ensure_platform_int (missing )
788
768
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
+
789
793
@property
790
794
def _index_as_unique (self ):
791
795
return not self .is_overlapping
0 commit comments