@@ -122,6 +122,7 @@ def setop_check(method):
122122
123123 @wraps (method )
124124 def wrapped (self , other , sort = False ):
125+ self ._validate_sort_keyword (sort )
125126 self ._assert_can_do_setop (other )
126127 other = ensure_index (other )
127128
@@ -131,14 +132,6 @@ def wrapped(self, other, sort=False):
131132 result = result .astype (self .dtype )
132133 return result
133134
134- if self ._is_non_comparable_own_type (other ):
135- # GH#19016: ensure set op will not return a prohibited dtype
136- raise TypeError (
137- "can only do set operations between two IntervalIndex "
138- "objects that are closed on the same side "
139- "and have compatible dtypes"
140- )
141-
142135 return method (self , other , sort )
143136
144137 return wrapped
@@ -956,11 +949,27 @@ def _format_space(self) -> str:
956949 # --------------------------------------------------------------------
957950 # Set Operations
958951
952+ def _assert_can_do_setop (self , other ):
953+ super ()._assert_can_do_setop (other )
954+
955+ if isinstance (other , IntervalIndex ) and self ._is_non_comparable_own_type (other ):
956+ # GH#19016: ensure set op will not return a prohibited dtype
957+ raise TypeError (
958+ "can only do set operations between two IntervalIndex "
959+ "objects that are closed on the same side "
960+ "and have compatible dtypes"
961+ )
962+
959963 @Appender (Index .intersection .__doc__ )
960964 @setop_check
961- def intersection (
962- self , other : "IntervalIndex" , sort : bool = False
963- ) -> "IntervalIndex" :
965+ def intersection (self , other , sort = False ) -> Index :
966+ self ._validate_sort_keyword (sort )
967+ self ._assert_can_do_setop (other )
968+ other , _ = self ._convert_can_do_setop (other )
969+
970+ if not isinstance (other , IntervalIndex ):
971+ return self .astype (object ).intersection (other )
972+
964973 if self .left .is_unique and self .right .is_unique :
965974 taken = self ._intersection_unique (other )
966975 elif other .left .is_unique and other .right .is_unique and self .isna ().sum () <= 1 :
0 commit comments