1
1
import numpy as np
2
2
import pytest
3
3
4
+ import pandas as pd
4
5
from pandas import Index , Series
5
6
import pandas ._testing as tm
6
7
from pandas .core .algorithms import safe_sort
7
8
8
9
9
10
class TestIndexSetOps :
11
+ @pytest .mark .parametrize (
12
+ "method" , ["union" , "intersection" , "difference" , "symmetric_difference" ]
13
+ )
14
+ def test_setops_disallow_true (self , method ):
15
+ idx1 = pd .Index (["a" , "b" ])
16
+ idx2 = pd .Index (["b" , "c" ])
17
+
18
+ with pytest .raises (ValueError , match = "The 'sort' keyword only takes" ):
19
+ getattr (idx1 , method )(idx2 , sort = True )
20
+
21
+ def test_setops_preserve_object_dtype (self ):
22
+ idx = pd .Index ([1 , 2 , 3 ], dtype = object )
23
+ result = idx .intersection (idx [1 :])
24
+ expected = idx [1 :]
25
+ tm .assert_index_equal (result , expected )
26
+
27
+ # if other is not monotonic increasing, intersection goes through
28
+ # a different route
29
+ result = idx .intersection (idx [1 :][::- 1 ])
30
+ tm .assert_index_equal (result , expected )
31
+
32
+ result = idx ._union (idx [1 :], sort = None )
33
+ expected = idx
34
+ tm .assert_index_equal (result , expected )
35
+
36
+ result = idx .union (idx [1 :], sort = None )
37
+ tm .assert_index_equal (result , expected )
38
+
39
+ # if other is not monotonic increasing, _union goes through
40
+ # a different route
41
+ result = idx ._union (idx [1 :][::- 1 ], sort = None )
42
+ tm .assert_index_equal (result , expected )
43
+
44
+ result = idx .union (idx [1 :][::- 1 ], sort = None )
45
+ tm .assert_index_equal (result , expected )
46
+
10
47
def test_union_base (self ):
11
48
index = Index ([0 , "a" , 1 , "b" , 2 , "c" ])
12
49
first = index [3 :]
@@ -28,6 +65,32 @@ def test_union_different_type_base(self, klass):
28
65
29
66
assert tm .equalContents (result , index )
30
67
68
+ def test_union_sort_other_incomparable (self ):
69
+ # https://github.com/pandas-dev/pandas/issues/24959
70
+ idx = pd .Index ([1 , pd .Timestamp ("2000" )])
71
+ # default (sort=None)
72
+ with tm .assert_produces_warning (RuntimeWarning ):
73
+ result = idx .union (idx [:1 ])
74
+
75
+ tm .assert_index_equal (result , idx )
76
+
77
+ # sort=None
78
+ with tm .assert_produces_warning (RuntimeWarning ):
79
+ result = idx .union (idx [:1 ], sort = None )
80
+ tm .assert_index_equal (result , idx )
81
+
82
+ # sort=False
83
+ result = idx .union (idx [:1 ], sort = False )
84
+ tm .assert_index_equal (result , idx )
85
+
86
+ @pytest .mark .xfail (reason = "Not implemented" )
87
+ def test_union_sort_other_incomparable_true (self ):
88
+ # TODO decide on True behaviour
89
+ # sort=True
90
+ idx = pd .Index ([1 , pd .Timestamp ("2000" )])
91
+ with pytest .raises (TypeError , match = ".*" ):
92
+ idx .union (idx [:1 ], sort = True )
93
+
31
94
@pytest .mark .parametrize ("sort" , [None , False ])
32
95
def test_intersection_base (self , sort ):
33
96
# (same results for py2 and py3 but sortedness not tested elsewhere)
@@ -50,6 +113,16 @@ def test_intersection_different_type_base(self, klass, sort):
50
113
result = first .intersection (klass (second .values ), sort = sort )
51
114
assert tm .equalContents (result , second )
52
115
116
+ def test_intersect_nosort (self ):
117
+ result = pd .Index (["c" , "b" , "a" ]).intersection (["b" , "a" ])
118
+ expected = pd .Index (["b" , "a" ])
119
+ tm .assert_index_equal (result , expected )
120
+
121
+ def test_intersection_equal_sort (self ):
122
+ idx = pd .Index (["c" , "a" , "b" ])
123
+ tm .assert_index_equal (idx .intersection (idx , sort = False ), idx )
124
+ tm .assert_index_equal (idx .intersection (idx , sort = None ), idx )
125
+
53
126
@pytest .mark .parametrize ("sort" , [None , False ])
54
127
def test_difference_base (self , sort ):
55
128
# (same results for py2 and py3 but sortedness not tested elsewhere)
0 commit comments