28
28
from pandas ._libs .period import Period
29
29
30
30
from pandas .core .indexes .base import Index , _index_shared_docs
31
+ from pandas .tseries .offsets import index_offsets_equal
31
32
from pandas .util ._decorators import Appender , cache_readonly
32
33
import pandas .core .dtypes .concat as _concat
33
34
import pandas .tseries .frequencies as frequencies
@@ -854,11 +855,7 @@ def _concat_same_dtype(self, to_concat, name):
854
855
new_data = np .concatenate ([c .asi8 for c in to_concat ])
855
856
return self ._simple_new (new_data , ** attribs )
856
857
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 ):
862
859
# to make our life easier, "sort" the two ranges
863
860
if self [0 ] <= other [0 ]:
864
861
left , right = self , other
@@ -869,10 +866,10 @@ def _intersect_ascending(self, other, **empty_params):
869
866
start = right [0 ]
870
867
871
868
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 ))]
874
871
875
- def _intersect_descending (self , other , ** empty_params ):
872
+ def _intersect_descending (self , other ):
876
873
# this is essentially a flip of _intersect_ascending
877
874
if self [0 ] >= other [0 ]:
878
875
left , right = self , other
@@ -883,17 +880,8 @@ def _intersect_descending(self, other, **empty_params):
883
880
end = right [- 1 ]
884
881
885
882
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 ))]
897
885
898
886
def intersection (self , other ):
899
887
"""
@@ -914,55 +902,44 @@ def intersection(self, other):
914
902
if self .equals (other ):
915
903
return self ._get_consensus_name (other )
916
904
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 ):
922
912
result = Index .intersection (self , other )
923
913
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 )):
927
917
result = Index .intersection (self , other )
928
- tz = getattr (self , 'tz' , None )
929
918
result = self ._shallow_copy (result ._values , name = result .name ,
930
- tz = tz , freq = None )
919
+ tz = getattr ( self , 'tz' , None ) )
931
920
if result .freq is None :
932
921
result .offset = frequencies .to_offset (result .inferred_freq )
933
922
return result
934
923
935
- lenghts = len (self ), len (other )
936
- if lenghts [0 ] == 0 :
937
- return self
938
- if lenghts [1 ] == 0 :
939
- return other
940
-
941
924
# handle intersecting things like this
942
925
# idx1 = pd.to_timedelta((1, 2, 3, 4, 5, 6, 7, 8), unit='s')
943
926
# 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 )):
946
929
return Index .intersection (self , other )
947
930
948
931
# coerce into same order
949
932
self_ascending = self .is_monotonic_increasing
950
933
if self_ascending != other .is_monotonic_increasing :
951
934
other = other .sort_values (ascending = self_ascending )
952
935
953
- # Thanks, PeriodIndex
954
- empty_params = {'freq' : getattr (self , 'freq' , None )}
955
-
956
936
if self_ascending :
957
- intersected = self ._intersect_ascending (other , ** empty_params )
937
+ intersected_slice = self ._intersect_ascending (other )
958
938
else :
959
- intersected = self ._intersect_descending (other , ** empty_params )
939
+ intersected_slice = self ._intersect_descending (other )
960
940
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 )
966
943
967
944
968
945
def _ensure_datetimelike_to_i8 (other ):
0 commit comments