Skip to content

Commit a12c28b

Browse files
jschendeltm9k1
authored andcommitted
TST: Fix improperly defined tests in test_interval_new.py (pandas-dev#23217)
1 parent de08242 commit a12c28b

File tree

1 file changed

+87
-110
lines changed

1 file changed

+87
-110
lines changed

pandas/tests/indexes/interval/test_interval_new.py

+87-110
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ def _compare_tuple_of_numpy_array(self, result, expected):
1919
tm.assert_numpy_array_equal(lidx, lidx_expected)
2020
tm.assert_numpy_array_equal(ridx, ridx_expected)
2121

22-
@pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither'])
2322
@pytest.mark.parametrize("side", ['right', 'left', 'both', 'neither'])
24-
def test_get_loc_interval(self, idx_side, side):
23+
def test_get_loc_interval(self, closed, side):
2524

26-
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side)
25+
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
2726

2827
for bound in [[0, 1], [1, 2], [2, 3], [3, 4],
2928
[0, 2], [2.5, 3], [-1, 4]]:
3029
# if get_loc is supplied an interval, it should only search
3130
# for exact matches, not overlaps or covers, else KeyError.
32-
if idx_side == side:
31+
if closed == side:
3332
if bound == [0, 1]:
3433
assert idx.get_loc(Interval(0, 1, closed=side)) == 0
3534
elif bound == [2, 3]:
@@ -41,9 +40,8 @@ def test_get_loc_interval(self, idx_side, side):
4140
with pytest.raises(KeyError):
4241
idx.get_loc(Interval(*bound, closed=side))
4342

44-
@pytest.mark.parametrize("idx_side", ['right', 'left', 'both', 'neither'])
4543
@pytest.mark.parametrize("scalar", [-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5])
46-
def test_get_loc_scalar(self, idx_side, scalar):
44+
def test_get_loc_scalar(self, closed, scalar):
4745

4846
# correct = {side: {query: answer}}.
4947
# If query is not in the dict, that query should raise a KeyError
@@ -52,12 +50,12 @@ def test_get_loc_scalar(self, idx_side, scalar):
5250
'both': {0: 0, 0.5: 0, 1: 0, 2: 1, 2.5: 1, 3: 1},
5351
'neither': {0.5: 0, 2.5: 1}}
5452

55-
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=idx_side)
53+
idx = IntervalIndex.from_tuples([(0, 1), (2, 3)], closed=closed)
5654

5755
# if get_loc is supplied a scalar, it should return the index of
5856
# the interval which contains the scalar, or KeyError.
59-
if scalar in correct[idx_side].keys():
60-
assert idx.get_loc(scalar) == correct[idx_side][scalar]
57+
if scalar in correct[closed].keys():
58+
assert idx.get_loc(scalar) == correct[closed][scalar]
6159
else:
6260
pytest.raises(KeyError, idx.get_loc, scalar)
6361

@@ -139,140 +137,119 @@ def test_slice_locs_with_ints_and_floats_succeeds(self):
139137
assert index.slice_locs(3, 4) == (1, 0)
140138
assert index.slice_locs(0, 4) == (3, 0)
141139

142-
@pytest.mark.parametrize("query", [[0, 1], [0, 2], [0, 3],
143-
[3, 1], [3, 4], [0, 4]])
144-
def test_slice_locs_with_ints_and_floats_fails(self, query):
145-
146-
# increasing overlapping
147-
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
148-
pytest.raises(KeyError, index.slice_locs, query)
149-
150-
# decreasing overlapping
151-
index = IntervalIndex.from_tuples([(2, 4), (1, 3), (0, 2)])
152-
pytest.raises(KeyError, index.slice_locs, query)
153-
154-
# sorted duplicates
155-
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4)])
156-
pytest.raises(KeyError, index.slice_locs, query)
157-
158-
# unsorted duplicates
159-
index = IntervalIndex.from_tuples([(0, 2), (2, 4), (0, 2)])
160-
pytest.raises(KeyError, index.slice_locs, query)
161-
162-
# another unsorted duplicates
163-
index = IntervalIndex.from_tuples([(0, 2), (0, 2), (2, 4), (1, 3)])
164-
pytest.raises(KeyError, index.slice_locs, query)
165-
166140
@pytest.mark.parametrize("query", [
167-
Interval(1, 3, closed='right'),
168-
Interval(1, 3, closed='left'),
169-
Interval(1, 3, closed='both'),
170-
Interval(1, 3, closed='neither'),
171-
Interval(1, 4, closed='right'),
172-
Interval(0, 4, closed='right'),
173-
Interval(1, 2, closed='right')])
174-
@pytest.mark.parametrize("expected_result", [1, -1, -1, -1, -1, -1, -1])
175-
def test_get_indexer_with_interval_single_queries(
176-
self, query, expected_result):
141+
[0, 1], [0, 2], [0, 3], [3, 1], [3, 4], [0, 4]])
142+
@pytest.mark.parametrize("tuples", [
143+
[(0, 2), (1, 3), (2, 4)], [(2, 4), (1, 3), (0, 2)],
144+
[(0, 2), (0, 2), (2, 4)], [(0, 2), (2, 4), (0, 2)],
145+
[(0, 2), (0, 2), (2, 4), (1, 3)]])
146+
def test_slice_locs_with_ints_and_floats_errors(self, tuples, query):
147+
index = IntervalIndex.from_tuples(tuples)
148+
with pytest.raises(KeyError):
149+
index.slice_locs(query)
150+
151+
@pytest.mark.parametrize('query, expected', [
152+
(Interval(1, 3, closed='right'), 1),
153+
(Interval(1, 3, closed='left'), -1),
154+
(Interval(1, 3, closed='both'), -1),
155+
(Interval(1, 3, closed='neither'), -1),
156+
(Interval(1, 4, closed='right'), -1),
157+
(Interval(0, 4, closed='right'), -1),
158+
(Interval(1, 2, closed='right'), -1)])
159+
def test_get_indexer_with_interval_single_queries(self, query, expected):
177160

178161
index = IntervalIndex.from_tuples(
179162
[(0, 2.5), (1, 3), (2, 4)], closed='right')
180163

181164
result = index.get_indexer([query])
182-
expect = np.array([expected_result], dtype='intp')
183-
tm.assert_numpy_array_equal(result, expect)
184-
185-
@pytest.mark.parametrize("query", [
186-
[Interval(2, 4, closed='right'), Interval(1, 3, closed='right')],
187-
[Interval(1, 3, closed='right'), Interval(0, 2, closed='right')],
188-
[Interval(1, 3, closed='right'), Interval(1, 3, closed='left')]])
189-
@pytest.mark.parametrize("expected_result", [[2, 1], [1, -1], [1, -1]])
190-
def test_get_indexer_with_interval_multiple_queries(
191-
self, query, expected_result):
165+
expected = np.array([expected], dtype='intp')
166+
tm.assert_numpy_array_equal(result, expected)
167+
168+
@pytest.mark.parametrize('query, expected', [
169+
([Interval(2, 4, closed='right'), Interval(1, 3, closed='right')],
170+
[2, 1]),
171+
([Interval(1, 3, closed='right'), Interval(0, 2, closed='right')],
172+
[1, -1]),
173+
([Interval(1, 3, closed='right'), Interval(1, 3, closed='left')],
174+
[1, -1])])
175+
def test_get_indexer_with_interval_multiple_queries(self, query, expected):
192176

193177
index = IntervalIndex.from_tuples(
194178
[(0, 2.5), (1, 3), (2, 4)], closed='right')
195179

196180
result = index.get_indexer(query)
197-
expect = np.array(expected_result, dtype='intp')
198-
tm.assert_numpy_array_equal(result, expect)
199-
200-
@pytest.mark.parametrize(
201-
"query",
202-
[-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])
203-
@pytest.mark.parametrize(
204-
"expected_result",
205-
[-1, -1, 0, 0, 1, 1, -1, -1, 2, 2, -1])
181+
expected = np.array(expected, dtype='intp')
182+
tm.assert_numpy_array_equal(result, expected)
183+
184+
@pytest.mark.parametrize('query, expected', [
185+
(-0.5, -1), (0, -1), (0.5, 0), (1, 0), (1.5, 1), (2, 1),
186+
(2.5, -1), (3, -1), (3.5, 2), (4, 2), (4.5, -1)])
206187
def test_get_indexer_with_ints_and_floats_single_queries(
207-
self, query, expected_result):
188+
self, query, expected):
208189

209190
index = IntervalIndex.from_tuples(
210191
[(0, 1), (1, 2), (3, 4)], closed='right')
211192

212193
result = index.get_indexer([query])
213-
expect = np.array([expected_result], dtype='intp')
214-
tm.assert_numpy_array_equal(result, expect)
215-
216-
@pytest.mark.parametrize(
217-
"query",
218-
[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]])
219-
@pytest.mark.parametrize(
220-
"expected_result",
221-
[[0, 1], [0, 1, -1], [0, 1, -1, 2], [0, 1, -1, 2, 1]])
194+
expected = np.array([expected], dtype='intp')
195+
tm.assert_numpy_array_equal(result, expected)
196+
197+
@pytest.mark.parametrize('query, expected', [
198+
([1, 2], [0, 1]),
199+
([1, 2, 3], [0, 1, -1]),
200+
([1, 2, 3, 4], [0, 1, -1, 2]),
201+
([1, 2, 3, 4, 2], [0, 1, -1, 2, 1])])
222202
def test_get_indexer_with_ints_and_floats_multiple_queries(
223-
self, query, expected_result):
203+
self, query, expected):
224204

225205
index = IntervalIndex.from_tuples(
226206
[(0, 1), (1, 2), (3, 4)], closed='right')
227207

228208
result = index.get_indexer(query)
229-
expect = np.array(expected_result, dtype='intp')
230-
tm.assert_numpy_array_equal(result, expect)
209+
expected = np.array(expected, dtype='intp')
210+
tm.assert_numpy_array_equal(result, expected)
231211

232212
index = IntervalIndex.from_tuples([(0, 2), (1, 3), (2, 4)])
233213
# TODO: @shoyer believes this should raise, master branch doesn't
234214

235-
@pytest.mark.parametrize(
236-
"query",
237-
[-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5])
238-
@pytest.mark.parametrize("expected_result", [
239-
(Int64Index([], dtype='int64'), np.array([0])),
240-
(Int64Index([0], dtype='int64'), np.array([])),
241-
(Int64Index([0], dtype='int64'), np.array([])),
242-
(Int64Index([0, 1], dtype='int64'), np.array([])),
243-
(Int64Index([0, 1], dtype='int64'), np.array([])),
244-
(Int64Index([0, 1, 2], dtype='int64'), np.array([])),
245-
(Int64Index([1, 2], dtype='int64'), np.array([])),
246-
(Int64Index([2], dtype='int64'), np.array([])),
247-
(Int64Index([2], dtype='int64'), np.array([])),
248-
(Int64Index([], dtype='int64'), np.array([0])),
249-
(Int64Index([], dtype='int64'), np.array([0]))])
215+
@pytest.mark.parametrize('query, expected', [
216+
(-0.5, (Int64Index([], dtype='int64'), np.array([0]))),
217+
(0, (Int64Index([0], dtype='int64'), np.array([]))),
218+
(0.5, (Int64Index([0], dtype='int64'), np.array([]))),
219+
(1, (Int64Index([0, 1], dtype='int64'), np.array([]))),
220+
(1.5, (Int64Index([0, 1], dtype='int64'), np.array([]))),
221+
(2, (Int64Index([0, 1, 2], dtype='int64'), np.array([]))),
222+
(2.5, (Int64Index([1, 2], dtype='int64'), np.array([]))),
223+
(3, (Int64Index([2], dtype='int64'), np.array([]))),
224+
(3.5, (Int64Index([2], dtype='int64'), np.array([]))),
225+
(4, (Int64Index([], dtype='int64'), np.array([0]))),
226+
(4.5, (Int64Index([], dtype='int64'), np.array([0])))])
250227
def test_get_indexer_non_unique_with_ints_and_floats_single_queries(
251-
self, query, expected_result):
228+
self, query, expected):
252229

253230
index = IntervalIndex.from_tuples(
254231
[(0, 2.5), (1, 3), (2, 4)], closed='left')
255232

256233
result = index.get_indexer_non_unique([query])
257-
tm.assert_numpy_array_equal(result, expected_result)
258-
259-
@pytest.mark.parametrize(
260-
"query",
261-
[[1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 2]])
262-
@pytest.mark.parametrize("expected_result", [
263-
(Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([])),
264-
(Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([])),
265-
(Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3])),
266-
(Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'),
267-
np.array([3]))])
234+
tm.assert_numpy_array_equal(result, expected)
235+
236+
@pytest.mark.parametrize('query, expected', [
237+
([1, 2], (Int64Index([0, 1, 0, 1, 2], dtype='int64'), np.array([]))),
238+
([1, 2, 3],
239+
(Int64Index([0, 1, 0, 1, 2, 2], dtype='int64'), np.array([]))),
240+
([1, 2, 3, 4],
241+
(Int64Index([0, 1, 0, 1, 2, 2, -1], dtype='int64'), np.array([3]))),
242+
([1, 2, 3, 4, 2],
243+
(Int64Index([0, 1, 0, 1, 2, 2, -1, 0, 1, 2], dtype='int64'),
244+
np.array([3])))])
268245
def test_get_indexer_non_unique_with_ints_and_floats_multiple_queries(
269-
self, query, expected_result):
246+
self, query, expected):
270247

271248
index = IntervalIndex.from_tuples(
272249
[(0, 2.5), (1, 3), (2, 4)], closed='left')
273250

274251
result = index.get_indexer_non_unique(query)
275-
tm.assert_numpy_array_equal(result, expected_result)
252+
tm.assert_numpy_array_equal(result, expected)
276253

277254
# TODO we may also want to test get_indexer for the case when
278255
# the intervals are duplicated, decreasing, non-monotonic, etc..
@@ -303,13 +280,13 @@ def test_contains_method(self):
303280
assert index.contains(0.5)
304281
assert index.contains(1)
305282

306-
assert index.contains(Interval(0, 1), closed='right')
307-
assert not index.contains(Interval(0, 1), closed='left')
308-
assert not index.contains(Interval(0, 1), closed='both')
309-
assert not index.contains(Interval(0, 2), closed='right')
283+
assert index.contains(Interval(0, 1, closed='right'))
284+
assert not index.contains(Interval(0, 1, closed='left'))
285+
assert not index.contains(Interval(0, 1, closed='both'))
286+
assert not index.contains(Interval(0, 2, closed='right'))
310287

311-
assert not index.contains(Interval(0, 3), closed='right')
312-
assert not index.contains(Interval(1, 3), closed='right')
288+
assert not index.contains(Interval(0, 3, closed='right'))
289+
assert not index.contains(Interval(1, 3, closed='right'))
313290

314291
assert not index.contains(20)
315292
assert not index.contains(-20)

0 commit comments

Comments
 (0)