12
12
ensure_platform_int ,
13
13
is_bool_dtype ,
14
14
is_datetime64_any_dtype ,
15
+ is_dtype_equal ,
15
16
is_float ,
16
17
is_float_dtype ,
17
18
is_integer ,
@@ -782,6 +783,9 @@ def join(self, other, how="left", level=None, return_indexers=False, sort=False)
782
783
return self ._apply_meta (result ), lidx , ridx
783
784
return self ._apply_meta (result )
784
785
786
+ # ------------------------------------------------------------------------
787
+ # Set Operation Methods
788
+
785
789
def _assert_can_do_setop (self , other ):
786
790
super ()._assert_can_do_setop (other )
787
791
@@ -799,6 +803,30 @@ def _wrap_setop_result(self, other, result):
799
803
result .name = name
800
804
return result
801
805
806
+ def intersection (self , other , sort = False ):
807
+ self ._validate_sort_keyword (sort )
808
+ self ._assert_can_do_setop (other )
809
+ res_name = get_op_result_name (self , other )
810
+ other = ensure_index (other )
811
+
812
+ if self .equals (other ):
813
+ return self ._get_reconciled_name_object (other )
814
+
815
+ if not is_dtype_equal (self .dtype , other .dtype ):
816
+ # TODO: fastpath for if we have a different PeriodDtype
817
+ this = self .astype ("O" )
818
+ other = other .astype ("O" )
819
+ return this .intersection (other , sort = sort )
820
+
821
+ i8self = Int64Index ._simple_new (self .asi8 )
822
+ i8other = Int64Index ._simple_new (other .asi8 )
823
+ i8result = i8self .intersection (i8other , sort = sort )
824
+
825
+ result = self ._shallow_copy (np .asarray (i8result , dtype = np .int64 ), name = res_name )
826
+ return result
827
+
828
+ # ------------------------------------------------------------------------
829
+
802
830
def _apply_meta (self , rawarr ):
803
831
if not isinstance (rawarr , PeriodIndex ):
804
832
rawarr = PeriodIndex ._simple_new (rawarr , freq = self .freq , name = self .name )
0 commit comments