@@ -10,17 +10,23 @@ def name(request):
10
10
return request .param
11
11
12
12
13
- class TestIntervalIndex :
13
+ def monotonic_index (start , end , dtype = 'int64' , closed = 'right' ):
14
+ return IntervalIndex .from_breaks (np .arange (start , end , dtype = dtype ),
15
+ closed = closed )
16
+
17
+
18
+ def empty_index (dtype = 'int64' , closed = 'right' ):
19
+ return IntervalIndex (np .array ([], dtype = dtype ), closed = closed )
14
20
15
- def create_index ( self , closed = 'right' ):
16
- return IntervalIndex . from_breaks ( range ( 11 ), closed = closed )
21
+
22
+ class TestIntervalIndex :
17
23
18
24
@pytest .mark .parametrize ("sort" , [None , False ])
19
25
def test_union (self , closed , sort ):
20
- index = self . create_index ( closed = closed )
21
- other = IntervalIndex . from_breaks ( range ( 5 , 13 ) , closed = closed )
26
+ index = monotonic_index ( 0 , 11 , closed = closed )
27
+ other = monotonic_index ( 5 , 13 , closed = closed )
22
28
23
- expected = IntervalIndex . from_breaks ( range ( 13 ) , closed = closed )
29
+ expected = monotonic_index ( 0 , 13 , closed = closed )
24
30
result = index [::- 1 ].union (other , sort = sort )
25
31
if sort is None :
26
32
tm .assert_index_equal (result , expected )
@@ -35,21 +41,21 @@ def test_union(self, closed, sort):
35
41
tm .assert_index_equal (index .union (index [:1 ], sort = sort ), index )
36
42
37
43
# GH 19101: empty result, same dtype
38
- index = IntervalIndex ( np . array ([], dtype = 'int64' ) , closed = closed )
44
+ index = empty_index ( dtype = 'int64' , closed = closed )
39
45
result = index .union (index , sort = sort )
40
46
tm .assert_index_equal (result , index )
41
47
42
48
# GH 19101: empty result, different dtypes
43
- other = IntervalIndex ( np . array ([], dtype = 'float64' ) , closed = closed )
49
+ other = empty_index ( dtype = 'float64' , closed = closed )
44
50
result = index .union (other , sort = sort )
45
51
tm .assert_index_equal (result , index )
46
52
47
53
@pytest .mark .parametrize ("sort" , [None , False ])
48
54
def test_intersection (self , closed , sort ):
49
- index = self . create_index ( closed = closed )
50
- other = IntervalIndex . from_breaks ( range ( 5 , 13 ) , closed = closed )
55
+ index = monotonic_index ( 0 , 11 , closed = closed )
56
+ other = monotonic_index ( 5 , 13 , closed = closed )
51
57
52
- expected = IntervalIndex . from_breaks ( range ( 5 , 11 ) , closed = closed )
58
+ expected = monotonic_index ( 5 , 11 , closed = closed )
53
59
result = index [::- 1 ].intersection (other , sort = sort )
54
60
if sort is None :
55
61
tm .assert_index_equal (result , expected )
@@ -63,14 +69,13 @@ def test_intersection(self, closed, sort):
63
69
tm .assert_index_equal (index .intersection (index , sort = sort ), index )
64
70
65
71
# GH 19101: empty result, same dtype
66
- other = IntervalIndex . from_breaks ( range ( 300 , 314 ) , closed = closed )
67
- expected = IntervalIndex ( np . array ([], dtype = 'int64' ) , closed = closed )
72
+ other = monotonic_index ( 300 , 314 , closed = closed )
73
+ expected = empty_index ( dtype = 'int64' , closed = closed )
68
74
result = index .intersection (other , sort = sort )
69
75
tm .assert_index_equal (result , expected )
70
76
71
77
# GH 19101: empty result, different dtypes
72
- breaks = np .arange (300 , 314 , dtype = 'float64' )
73
- other = IntervalIndex .from_breaks (breaks , closed = closed )
78
+ other = monotonic_index (300 , 314 , dtype = 'float64' , closed = closed )
74
79
result = index .intersection (other , sort = sort )
75
80
tm .assert_index_equal (result , expected )
76
81
@@ -101,7 +106,7 @@ def test_difference(self, closed, sort):
101
106
102
107
# GH 19101: empty result, same dtype
103
108
result = index .difference (index , sort = sort )
104
- expected = IntervalIndex ( np . array ([], dtype = 'int64' ) , closed = closed )
109
+ expected = empty_index ( dtype = 'int64' , closed = closed )
105
110
tm .assert_index_equal (result , expected )
106
111
107
112
# GH 19101: empty result, different dtypes
@@ -112,7 +117,7 @@ def test_difference(self, closed, sort):
112
117
113
118
@pytest .mark .parametrize ("sort" , [None , False ])
114
119
def test_symmetric_difference (self , closed , sort ):
115
- index = self . create_index ( closed = closed )
120
+ index = monotonic_index ( 0 , 11 , closed = closed )
116
121
result = index [1 :].symmetric_difference (index [:- 1 ], sort = sort )
117
122
expected = IntervalIndex ([index [0 ], index [- 1 ]])
118
123
if sort is None :
@@ -121,7 +126,7 @@ def test_symmetric_difference(self, closed, sort):
121
126
122
127
# GH 19101: empty result, same dtype
123
128
result = index .symmetric_difference (index , sort = sort )
124
- expected = IntervalIndex ( np . array ([], dtype = 'int64' ) , closed = closed )
129
+ expected = empty_index ( dtype = 'int64' , closed = closed )
125
130
if sort is None :
126
131
tm .assert_index_equal (result , expected )
127
132
assert tm .equalContents (result , expected )
@@ -136,7 +141,7 @@ def test_symmetric_difference(self, closed, sort):
136
141
'union' , 'intersection' , 'difference' , 'symmetric_difference' ])
137
142
@pytest .mark .parametrize ("sort" , [None , False ])
138
143
def test_set_operation_errors (self , closed , op_name , sort ):
139
- index = self . create_index ( closed = closed )
144
+ index = monotonic_index ( 0 , 11 , closed = closed )
140
145
set_op = getattr (index , op_name )
141
146
142
147
# non-IntervalIndex
@@ -149,7 +154,7 @@ def test_set_operation_errors(self, closed, op_name, sort):
149
154
msg = ('can only do set operations between two IntervalIndex objects '
150
155
'that are closed on the same side' )
151
156
for other_closed in {'right' , 'left' , 'both' , 'neither' } - {closed }:
152
- other = self . create_index ( closed = other_closed )
157
+ other = monotonic_index ( 0 , 11 , closed = other_closed )
153
158
with pytest .raises (ValueError , match = msg ):
154
159
set_op (other , sort = sort )
155
160
0 commit comments