@@ -697,12 +697,28 @@ def _assert_can_do_setop(self, other):
697697 if isinstance (other , PeriodIndex ) and self .freq != other .freq :
698698 raise raise_on_incompatible (self , other )
699699
700- def intersection (self , other , sort = False ):
700+ def _setop (self , other , sort , opname : str ):
701+ """
702+ Perform a set operation by dispatching to the Int64Index implementation.
703+ """
701704 self ._validate_sort_keyword (sort )
702705 self ._assert_can_do_setop (other )
703706 res_name = get_op_result_name (self , other )
704707 other = ensure_index (other )
705708
709+ i8self = Int64Index ._simple_new (self .asi8 )
710+ i8other = Int64Index ._simple_new (other .asi8 )
711+ i8result = getattr (i8self , opname )(i8other , sort = sort )
712+
713+ parr = type (self ._data )(np .asarray (i8result , dtype = np .int64 ), dtype = self .dtype )
714+ result = type (self )._simple_new (parr , name = res_name )
715+ return result
716+
717+ def intersection (self , other , sort = False ):
718+ self ._validate_sort_keyword (sort )
719+ self ._assert_can_do_setop (other )
720+ other = ensure_index (other )
721+
706722 if self .equals (other ):
707723 return self ._get_reconciled_name_object (other )
708724
@@ -712,35 +728,24 @@ def intersection(self, other, sort=False):
712728 other = other .astype ("O" )
713729 return this .intersection (other , sort = sort )
714730
715- i8self = Int64Index ._simple_new (self .asi8 )
716- i8other = Int64Index ._simple_new (other .asi8 )
717- i8result = i8self .intersection (i8other , sort = sort )
718-
719- result = self ._shallow_copy (np .asarray (i8result , dtype = np .int64 ), name = res_name )
720- return result
731+ return self ._setop (other , sort , opname = "intersection" )
721732
722733 def difference (self , other , sort = None ):
723734 self ._validate_sort_keyword (sort )
724735 self ._assert_can_do_setop (other )
725- res_name = get_op_result_name (self , other )
726736 other = ensure_index (other )
727737
728738 if self .equals (other ):
729739 # pass an empty PeriodArray with the appropriate dtype
730- return self . _shallow_copy (self ._data [:0 ])
740+ return type ( self ). _simple_new (self ._data [:0 ], name = self . name )
731741
732742 if is_object_dtype (other ):
733743 return self .astype (object ).difference (other ).astype (self .dtype )
734744
735745 elif not is_dtype_equal (self .dtype , other .dtype ):
736746 return self
737747
738- i8self = Int64Index ._simple_new (self .asi8 )
739- i8other = Int64Index ._simple_new (other .asi8 )
740- i8result = i8self .difference (i8other , sort = sort )
741-
742- result = self ._shallow_copy (np .asarray (i8result , dtype = np .int64 ), name = res_name )
743- return result
748+ return self ._setop (other , sort , opname = "difference" )
744749
745750 def _union (self , other , sort ):
746751 if not len (other ) or self .equals (other ) or not len (self ):
@@ -754,13 +759,7 @@ def _union(self, other, sort):
754759 other = other .astype ("O" )
755760 return this ._union (other , sort = sort )
756761
757- i8self = Int64Index ._simple_new (self .asi8 )
758- i8other = Int64Index ._simple_new (other .asi8 )
759- i8result = i8self ._union (i8other , sort = sort )
760-
761- res_name = get_op_result_name (self , other )
762- result = self ._shallow_copy (np .asarray (i8result , dtype = np .int64 ), name = res_name )
763- return result
762+ return self ._setop (other , sort , opname = "_union" )
764763
765764 # ------------------------------------------------------------------------
766765
0 commit comments