1
+ from datetime import datetime
2
+
1
3
import numpy as np
2
4
import pytest
3
5
@@ -83,14 +85,21 @@ def test_union_sort_other_incomparable(self):
83
85
result = idx .union (idx [:1 ], sort = False )
84
86
tm .assert_index_equal (result , idx )
85
87
86
- @pytest .mark .xfail (reason = "Not implemented " )
88
+ @pytest .mark .xfail (reason = "GH#25151 need to decide on True behavior " )
87
89
def test_union_sort_other_incomparable_true (self ):
88
90
# TODO decide on True behaviour
89
91
# sort=True
90
92
idx = Index ([1 , pd .Timestamp ("2000" )])
91
93
with pytest .raises (TypeError , match = ".*" ):
92
94
idx .union (idx [:1 ], sort = True )
93
95
96
+ @pytest .mark .xfail (reason = "GH#25151 need to decide on True behavior" )
97
+ def test_intersection_equal_sort_true (self ):
98
+ # TODO decide on True behaviour
99
+ idx = Index (["c" , "a" , "b" ])
100
+ sorted_ = Index (["a" , "b" , "c" ])
101
+ tm .assert_index_equal (idx .intersection (idx , sort = True ), sorted_ )
102
+
94
103
def test_intersection_base (self , sort ):
95
104
# (same results for py2 and py3 but sortedness not tested elsewhere)
96
105
index = Index ([0 , "a" , 1 , "b" , 2 , "c" ])
@@ -111,7 +120,7 @@ def test_intersection_different_type_base(self, klass, sort):
111
120
result = first .intersection (klass (second .values ), sort = sort )
112
121
assert tm .equalContents (result , second )
113
122
114
- def test_intersect_nosort (self ):
123
+ def test_intersection_nosort (self ):
115
124
result = Index (["c" , "b" , "a" ]).intersection (["b" , "a" ])
116
125
expected = Index (["b" , "a" ])
117
126
tm .assert_index_equal (result , expected )
@@ -121,6 +130,28 @@ def test_intersection_equal_sort(self):
121
130
tm .assert_index_equal (idx .intersection (idx , sort = False ), idx )
122
131
tm .assert_index_equal (idx .intersection (idx , sort = None ), idx )
123
132
133
+ def test_intersection_str_dates (self , sort ):
134
+ dt_dates = [datetime (2012 , 2 , 9 ), datetime (2012 , 2 , 22 )]
135
+
136
+ i1 = Index (dt_dates , dtype = object )
137
+ i2 = Index (["aa" ], dtype = object )
138
+ result = i2 .intersection (i1 , sort = sort )
139
+
140
+ assert len (result ) == 0
141
+
142
+ @pytest .mark .parametrize (
143
+ "index2,expected_arr" ,
144
+ [(Index (["B" , "D" ]), ["B" ]), (Index (["B" , "D" , "A" ]), ["A" , "B" , "A" ])],
145
+ )
146
+ def test_intersection_non_monotonic_non_unique (self , index2 , expected_arr , sort ):
147
+ # non-monotonic non-unique
148
+ index1 = Index (["A" , "B" , "A" , "C" ])
149
+ expected = Index (expected_arr , dtype = "object" )
150
+ result = index1 .intersection (index2 , sort = sort )
151
+ if sort is None :
152
+ expected = expected .sort_values ()
153
+ tm .assert_index_equal (result , expected )
154
+
124
155
def test_difference_base (self , sort ):
125
156
# (same results for py2 and py3 but sortedness not tested elsewhere)
126
157
index = Index ([0 , "a" , 1 , "b" , 2 , "c" ])
@@ -142,3 +173,74 @@ def test_symmetric_difference(self):
142
173
result = first .symmetric_difference (second )
143
174
expected = Index ([0 , 1 , 2 , "a" , "c" ])
144
175
tm .assert_index_equal (result , expected )
176
+
177
+ @pytest .mark .parametrize (
178
+ "method,expected,sort" ,
179
+ [
180
+ (
181
+ "intersection" ,
182
+ np .array (
183
+ [(1 , "A" ), (2 , "A" ), (1 , "B" ), (2 , "B" )],
184
+ dtype = [("num" , int ), ("let" , "a1" )],
185
+ ),
186
+ False ,
187
+ ),
188
+ (
189
+ "intersection" ,
190
+ np .array (
191
+ [(1 , "A" ), (1 , "B" ), (2 , "A" ), (2 , "B" )],
192
+ dtype = [("num" , int ), ("let" , "a1" )],
193
+ ),
194
+ None ,
195
+ ),
196
+ (
197
+ "union" ,
198
+ np .array (
199
+ [(1 , "A" ), (1 , "B" ), (1 , "C" ), (2 , "A" ), (2 , "B" ), (2 , "C" )],
200
+ dtype = [("num" , int ), ("let" , "a1" )],
201
+ ),
202
+ None ,
203
+ ),
204
+ ],
205
+ )
206
+ def test_tuple_union_bug (self , method , expected , sort ):
207
+ index1 = Index (
208
+ np .array (
209
+ [(1 , "A" ), (2 , "A" ), (1 , "B" ), (2 , "B" )],
210
+ dtype = [("num" , int ), ("let" , "a1" )],
211
+ )
212
+ )
213
+ index2 = Index (
214
+ np .array (
215
+ [(1 , "A" ), (2 , "A" ), (1 , "B" ), (2 , "B" ), (1 , "C" ), (2 , "C" )],
216
+ dtype = [("num" , int ), ("let" , "a1" )],
217
+ )
218
+ )
219
+
220
+ result = getattr (index1 , method )(index2 , sort = sort )
221
+ assert result .ndim == 1
222
+
223
+ expected = Index (expected )
224
+ tm .assert_index_equal (result , expected )
225
+
226
+ @pytest .mark .parametrize ("first_list" , [list ("ba" ), list ()])
227
+ @pytest .mark .parametrize ("second_list" , [list ("ab" ), list ()])
228
+ @pytest .mark .parametrize (
229
+ "first_name, second_name, expected_name" ,
230
+ [("A" , "B" , None ), (None , "B" , None ), ("A" , None , None )],
231
+ )
232
+ def test_union_name_preservation (
233
+ self , first_list , second_list , first_name , second_name , expected_name , sort
234
+ ):
235
+ first = Index (first_list , name = first_name )
236
+ second = Index (second_list , name = second_name )
237
+ union = first .union (second , sort = sort )
238
+
239
+ vals = set (first_list ).union (second_list )
240
+
241
+ if sort is None and len (first_list ) > 0 and len (second_list ) > 0 :
242
+ expected = Index (sorted (vals ), name = expected_name )
243
+ tm .assert_index_equal (union , expected )
244
+ else :
245
+ expected = Index (vals , name = expected_name )
246
+ tm .equalContents (union , expected )
0 commit comments