Skip to content

Commit 0218eee

Browse files
committed
Functionalize some indexes
1 parent e79bdc6 commit 0218eee

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

pandas/tests/indexes/interval/test_setops.py

+25-20
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@ def name(request):
1010
return request.param
1111

1212

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)
1420

15-
def create_index(self, closed='right'):
16-
return IntervalIndex.from_breaks(range(11), closed=closed)
21+
22+
class TestIntervalIndex:
1723

1824
@pytest.mark.parametrize("sort", [None, False])
1925
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)
2228

23-
expected = IntervalIndex.from_breaks(range(13), closed=closed)
29+
expected = monotonic_index(0, 13, closed=closed)
2430
result = index[::-1].union(other, sort=sort)
2531
if sort is None:
2632
tm.assert_index_equal(result, expected)
@@ -35,21 +41,21 @@ def test_union(self, closed, sort):
3541
tm.assert_index_equal(index.union(index[:1], sort=sort), index)
3642

3743
# GH 19101: empty result, same dtype
38-
index = IntervalIndex(np.array([], dtype='int64'), closed=closed)
44+
index = empty_index(dtype='int64', closed=closed)
3945
result = index.union(index, sort=sort)
4046
tm.assert_index_equal(result, index)
4147

4248
# GH 19101: empty result, different dtypes
43-
other = IntervalIndex(np.array([], dtype='float64'), closed=closed)
49+
other = empty_index(dtype='float64', closed=closed)
4450
result = index.union(other, sort=sort)
4551
tm.assert_index_equal(result, index)
4652

4753
@pytest.mark.parametrize("sort", [None, False])
4854
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)
5157

52-
expected = IntervalIndex.from_breaks(range(5, 11), closed=closed)
58+
expected = monotonic_index(5, 11, closed=closed)
5359
result = index[::-1].intersection(other, sort=sort)
5460
if sort is None:
5561
tm.assert_index_equal(result, expected)
@@ -63,14 +69,13 @@ def test_intersection(self, closed, sort):
6369
tm.assert_index_equal(index.intersection(index, sort=sort), index)
6470

6571
# 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)
6874
result = index.intersection(other, sort=sort)
6975
tm.assert_index_equal(result, expected)
7076

7177
# 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)
7479
result = index.intersection(other, sort=sort)
7580
tm.assert_index_equal(result, expected)
7681

@@ -101,7 +106,7 @@ def test_difference(self, closed, sort):
101106

102107
# GH 19101: empty result, same dtype
103108
result = index.difference(index, sort=sort)
104-
expected = IntervalIndex(np.array([], dtype='int64'), closed=closed)
109+
expected = empty_index(dtype='int64', closed=closed)
105110
tm.assert_index_equal(result, expected)
106111

107112
# GH 19101: empty result, different dtypes
@@ -112,7 +117,7 @@ def test_difference(self, closed, sort):
112117

113118
@pytest.mark.parametrize("sort", [None, False])
114119
def test_symmetric_difference(self, closed, sort):
115-
index = self.create_index(closed=closed)
120+
index = monotonic_index(0, 11, closed=closed)
116121
result = index[1:].symmetric_difference(index[:-1], sort=sort)
117122
expected = IntervalIndex([index[0], index[-1]])
118123
if sort is None:
@@ -121,7 +126,7 @@ def test_symmetric_difference(self, closed, sort):
121126

122127
# GH 19101: empty result, same dtype
123128
result = index.symmetric_difference(index, sort=sort)
124-
expected = IntervalIndex(np.array([], dtype='int64'), closed=closed)
129+
expected = empty_index(dtype='int64', closed=closed)
125130
if sort is None:
126131
tm.assert_index_equal(result, expected)
127132
assert tm.equalContents(result, expected)
@@ -136,7 +141,7 @@ def test_symmetric_difference(self, closed, sort):
136141
'union', 'intersection', 'difference', 'symmetric_difference'])
137142
@pytest.mark.parametrize("sort", [None, False])
138143
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)
140145
set_op = getattr(index, op_name)
141146

142147
# non-IntervalIndex
@@ -149,7 +154,7 @@ def test_set_operation_errors(self, closed, op_name, sort):
149154
msg = ('can only do set operations between two IntervalIndex objects '
150155
'that are closed on the same side')
151156
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)
153158
with pytest.raises(ValueError, match=msg):
154159
set_op(other, sort=sort)
155160

0 commit comments

Comments
 (0)