11
11
from pandas ._libs import lib
12
12
from pandas ._libs .interval import Interval , IntervalMixin , IntervalTree
13
13
from pandas ._libs .tslibs import BaseOffset , Timedelta , Timestamp , to_offset
14
- from pandas ._typing import AnyArrayLike , DtypeObj , Label
14
+ from pandas ._typing import DtypeObj , Label
15
15
from pandas .errors import InvalidIndexError
16
16
from pandas .util ._decorators import Appender , cache_readonly
17
17
from pandas .util ._exceptions import rewrite_exception
@@ -652,9 +652,8 @@ def _get_indexer(
652
652
if self .equals (target ):
653
653
return np .arange (len (self ), dtype = "intp" )
654
654
655
- if self ._is_non_comparable_own_type (target ):
656
- # different closed or incompatible subtype -> no matches
657
- return np .repeat (np .intp (- 1 ), len (target ))
655
+ if not self ._should_compare (target ):
656
+ return self ._get_indexer_non_comparable (target , method , unique = True )
658
657
659
658
# non-overlapping -> at most one match per interval in target
660
659
# want exact matches -> need both left/right to match, so defer to
@@ -678,32 +677,22 @@ def _get_indexer(
678
677
return ensure_platform_int (indexer )
679
678
680
679
@Appender (_index_shared_docs ["get_indexer_non_unique" ] % _index_doc_kwargs )
681
- def get_indexer_non_unique (
682
- self , target : AnyArrayLike
683
- ) -> Tuple [np .ndarray , np .ndarray ]:
684
- target_as_index = ensure_index (target )
685
-
686
- # check that target_as_index IntervalIndex is compatible
687
- if isinstance (target_as_index , IntervalIndex ):
688
-
689
- if self ._is_non_comparable_own_type (target_as_index ):
690
- # different closed or incompatible subtype -> no matches
691
- return (
692
- np .repeat (- 1 , len (target_as_index )),
693
- np .arange (len (target_as_index )),
694
- )
695
-
696
- if is_object_dtype (target_as_index ) or isinstance (
697
- target_as_index , IntervalIndex
698
- ):
699
- # target_as_index might contain intervals: defer elementwise to get_loc
700
- return self ._get_indexer_pointwise (target_as_index )
680
+ def get_indexer_non_unique (self , target : Index ) -> Tuple [np .ndarray , np .ndarray ]:
681
+ target = ensure_index (target )
682
+
683
+ if isinstance (target , IntervalIndex ) and not self ._should_compare (target ):
684
+ # different closed or incompatible subtype -> no matches
685
+ return self ._get_indexer_non_comparable (target , None , unique = False )
686
+
687
+ elif is_object_dtype (target .dtype ) or isinstance (target , IntervalIndex ):
688
+ # target might contain intervals: defer elementwise to get_loc
689
+ return self ._get_indexer_pointwise (target )
701
690
702
691
else :
703
- target_as_index = self . _maybe_convert_i8 ( target_as_index )
704
- indexer , missing = self . _engine . get_indexer_non_unique (
705
- target_as_index . values
706
- )
692
+ # Note: this case behaves differently from other Index subclasses
693
+ # because IntervalIndex does partial-int indexing
694
+ target = self . _maybe_convert_i8 ( target )
695
+ indexer , missing = self . _engine . get_indexer_non_unique ( target . values )
707
696
708
697
return ensure_platform_int (indexer ), ensure_platform_int (missing )
709
698
@@ -789,16 +778,6 @@ def _should_compare(self, other) -> bool:
789
778
return False
790
779
return other .closed == self .closed
791
780
792
- # TODO: use should_compare and get rid of _is_non_comparable_own_type
793
- def _is_non_comparable_own_type (self , other : "IntervalIndex" ) -> bool :
794
- # different closed or incompatible subtype -> no matches
795
-
796
- # TODO: once closed is part of IntervalDtype, we can just define
797
- # is_comparable_dtype GH#19371
798
- if self .closed != other .closed :
799
- return True
800
- return not self ._is_comparable_dtype (other .dtype )
801
-
802
781
# --------------------------------------------------------------------
803
782
804
783
@cache_readonly
@@ -938,7 +917,7 @@ def _format_space(self) -> str:
938
917
def _assert_can_do_setop (self , other ):
939
918
super ()._assert_can_do_setop (other )
940
919
941
- if isinstance (other , IntervalIndex ) and self ._is_non_comparable_own_type (other ):
920
+ if isinstance (other , IntervalIndex ) and not self ._should_compare (other ):
942
921
# GH#19016: ensure set op will not return a prohibited dtype
943
922
raise TypeError (
944
923
"can only do set operations between two IntervalIndex "
0 commit comments