2828from pandas ._libs .period import Period
2929
3030from pandas .core .indexes .base import Index , _index_shared_docs
31+ from pandas .tseries .offsets import index_offsets_equal
3132from pandas .util ._decorators import Appender , cache_readonly
3233import pandas .core .dtypes .concat as _concat
3334import pandas .tseries .frequencies as frequencies
@@ -854,11 +855,7 @@ def _concat_same_dtype(self, to_concat, name):
854855 new_data = np .concatenate ([c .asi8 for c in to_concat ])
855856 return self ._simple_new (new_data , ** attribs )
856857
857- def _slice_by_value (self , start , end ):
858- sliced = self .values [slice (* self .slice_locs (start , end ))]
859- return self ._shallow_copy (sliced )
860-
861- def _intersect_ascending (self , other , ** empty_params ):
858+ def _intersect_ascending (self , other ):
862859 # to make our life easier, "sort" the two ranges
863860 if self [0 ] <= other [0 ]:
864861 left , right = self , other
@@ -869,10 +866,10 @@ def _intersect_ascending(self, other, **empty_params):
869866 start = right [0 ]
870867
871868 if end < start :
872- return type ( self )( data = [], ** empty_params )
873- return left ._slice_by_value ( start , end )
869+ return []
870+ return left .values [ slice ( * left . slice_locs ( start , end ))]
874871
875- def _intersect_descending (self , other , ** empty_params ):
872+ def _intersect_descending (self , other ):
876873 # this is essentially a flip of _intersect_ascending
877874 if self [0 ] >= other [0 ]:
878875 left , right = self , other
@@ -883,17 +880,8 @@ def _intersect_descending(self, other, **empty_params):
883880 end = right [- 1 ]
884881
885882 if end > start :
886- return type (self )(data = [], ** empty_params )
887- return left ._slice_by_value (start , end )
888-
889- def _offsets_equal (self , other ):
890- offsets_equal = True
891- self_offset = getattr (self , 'offset' , None )
892- other_offset = getattr (other , 'offset' , None )
893- if self_offset is None or other_offset is None or \
894- self_offset != other_offset or other_offset .isAnchored ():
895- offsets_equal = False
896- return offsets_equal
883+ return Index ()
884+ return left .values [slice (* left .slice_locs (start , end ))]
897885
898886 def intersection (self , other ):
899887 """
@@ -914,55 +902,44 @@ def intersection(self, other):
914902 if self .equals (other ):
915903 return self ._get_consensus_name (other )
916904
917- if not isinstance (other , DatetimeIndexOpsMixin ):
918- try :
919- other = DatetimeIndexOpsMixin (other )
920- except (TypeError , ValueError ):
921- pass
905+ lengths = len (self ), len (other )
906+ if lengths [0 ] == 0 :
907+ return self
908+ if lengths [1 ] == 0 :
909+ return other
910+
911+ if not isinstance (other , Index ):
922912 result = Index .intersection (self , other )
923913 return result
924- elif (self . _offsets_equal ( other ) or
925- (not self .is_strictly_monotonic or
926- not other .is_strictly_monotonic )):
914+ elif (index_offsets_equal ( self , other ) or
915+ (not self ._is_strictly_monotonic or
916+ not other ._is_strictly_monotonic )):
927917 result = Index .intersection (self , other )
928- tz = getattr (self , 'tz' , None )
929918 result = self ._shallow_copy (result ._values , name = result .name ,
930- tz = tz , freq = None )
919+ tz = getattr ( self , 'tz' , None ) )
931920 if result .freq is None :
932921 result .offset = frequencies .to_offset (result .inferred_freq )
933922 return result
934923
935- lenghts = len (self ), len (other )
936- if lenghts [0 ] == 0 :
937- return self
938- if lenghts [1 ] == 0 :
939- return other
940-
941924 # handle intersecting things like this
942925 # idx1 = pd.to_timedelta((1, 2, 3, 4, 5, 6, 7, 8), unit='s')
943926 # idx2 = pd.to_timedelta((2, 3, 4, 8), unit='s')
944- if lenghts [0 ] != lenghts [1 ] and (
945- max (self ) != max (other ) or min (self ) != min (other )):
927+ if lengths [0 ] != lengths [1 ] and (
928+ max (self ) != max (other ) or min (self ) != min (other )):
946929 return Index .intersection (self , other )
947930
948931 # coerce into same order
949932 self_ascending = self .is_monotonic_increasing
950933 if self_ascending != other .is_monotonic_increasing :
951934 other = other .sort_values (ascending = self_ascending )
952935
953- # Thanks, PeriodIndex
954- empty_params = {'freq' : getattr (self , 'freq' , None )}
955-
956936 if self_ascending :
957- intersected = self ._intersect_ascending (other , ** empty_params )
937+ intersected_slice = self ._intersect_ascending (other )
958938 else :
959- intersected = self ._intersect_descending (other , ** empty_params )
939+ intersected_slice = self ._intersect_descending (other )
960940
961- name = self .name
962- if self .name != other .name :
963- name = None
964- intersected .name = name
965- return intersected
941+ intersected = self ._shallow_copy (intersected_slice )
942+ return intersected ._get_consensus_name (other )
966943
967944
968945def _ensure_datetimelike_to_i8 (other ):
0 commit comments