@@ -74,3 +74,74 @@ def test_intersection_bug_1708(self):
74
74
result = index_1 & index_2
75
75
expected = timedelta_range ('1 day 01:00:00' , periods = 3 , freq = 'h' )
76
76
tm .assert_index_equal (result , expected )
77
+
78
+
79
+ def test_intersection_intersects_ascending ():
80
+ idx1 = pd .to_timedelta (range (2 , 6 ), unit = 's' )
81
+ idx2 = pd .to_timedelta (range (3 ), unit = 's' )
82
+ result = idx1 .intersection (idx2 )
83
+ assert result .equals (TimedeltaIndex (['00:00:02' ]))
84
+
85
+ # test that it works both ways
86
+ idx1 = pd .to_timedelta (range (3 ), unit = 's' )
87
+ idx2 = pd .to_timedelta (range (2 , 6 ), unit = 's' )
88
+ result = idx1 .intersection (idx2 )
89
+ assert result .equals (TimedeltaIndex (['00:00:02' ]))
90
+
91
+
92
+ def test_intersection_intersects_descending ():
93
+ # GH 17391
94
+ idx1 = pd .to_timedelta (range (6 , 3 , - 1 ), unit = 's' )
95
+ idx2 = pd .to_timedelta (range (5 , 1 , - 1 ), unit = 's' )
96
+ result = idx1 .intersection (idx2 )
97
+ expected = TimedeltaIndex (['00:00:05' , '00:00:04' ],
98
+ dtype = 'timedelta64[ns]' )
99
+ assert result .equals (expected )
100
+
101
+ # test it works both ways
102
+ idx1 = pd .to_timedelta (range (5 , 1 , - 1 ), unit = 's' )
103
+ idx2 = pd .to_timedelta (range (6 , 3 , - 1 ), unit = 's' )
104
+ result = idx1 .intersection (idx2 )
105
+ assert result .equals (expected )
106
+
107
+
108
+ def test_intersection_intersects_descending_no_intersect ():
109
+ idx1 = pd .to_timedelta (range (6 , 4 , - 1 ), unit = 's' )
110
+ idx2 = pd .to_timedelta (range (4 , 1 , - 1 ), unit = 's' )
111
+ result = idx1 .intersection (idx2 )
112
+ assert len (result ) == 0
113
+
114
+
115
+ def test_intersection_intersects_len_1 ():
116
+ idx1 = pd .to_timedelta (range (1 , 2 ), unit = 's' )
117
+ idx2 = pd .to_timedelta (range (1 , 0 , - 1 ), unit = 's' )
118
+ intersection = idx1 .intersection (idx2 )
119
+ expected = TimedeltaIndex (['00:00:01' ],
120
+ dtype = 'timedelta64[ns]' )
121
+ tm .assert_index_equal (intersection , expected )
122
+
123
+
124
+ def test_intersection_can_intersect_self ():
125
+ idx = pd .to_timedelta (range (1 , 2 ), unit = 's' )
126
+ result = idx .intersection (idx )
127
+ tm .assert_index_equal (idx , result )
128
+
129
+
130
+ def test_intersection_not_sorted ():
131
+ idx1 = pd .to_timedelta ((1 , 3 , 2 , 5 , 4 ), unit = 's' )
132
+ idx2 = pd .to_timedelta ((1 , 2 , 3 , 5 , 4 ), unit = 's' )
133
+ result = idx1 .intersection (idx2 )
134
+ expected = idx1
135
+ tm .assert_index_equal (result , expected )
136
+
137
+
138
+ def test_intersection_not_unique ():
139
+ idx1 = pd .to_timedelta ((1 , 2 , 2 , 3 , 3 , 5 ), unit = 's' )
140
+ idx2 = pd .to_timedelta ((1 , 2 , 3 , 4 ), unit = 's' )
141
+ result = idx1 .intersection (idx2 )
142
+ expected = pd .to_timedelta ((1 , 2 , 2 , 3 , 3 ), unit = 's' )
143
+ tm .assert_index_equal (result , expected )
144
+
145
+ result = idx2 .intersection (idx1 )
146
+ expected = pd .to_timedelta ((1 , 2 , 2 , 3 , 3 ), unit = 's' )
147
+ tm .assert_index_equal (result , expected )
0 commit comments