Skip to content

Commit 6af5861

Browse files
authored
TST: Use more pytest fixtures (#53567)
* TST: Use more fixtures * Use more fixtures in test_indexing_slow * Move addition compression_to_extension * Use more fixture in sparse test_indexing * fixturize libsparse
1 parent 2a2002d commit 6af5861

15 files changed

+367
-324
lines changed

pandas/tests/arrays/sparse/test_indexing.py

+18-11
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
66
import pandas._testing as tm
77
from pandas.core.arrays.sparse import SparseArray
88

9-
arr_data = np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
10-
arr = SparseArray(arr_data)
9+
10+
@pytest.fixture
11+
def arr_data():
12+
return np.array([np.nan, np.nan, 1, 2, 3, np.nan, 4, 5, np.nan, 6])
13+
14+
15+
@pytest.fixture
16+
def arr(arr_data):
17+
return SparseArray(arr_data)
1118

1219

1320
class TestGetitem:
14-
def test_getitem(self):
21+
def test_getitem(self, arr):
1522
dense = arr.to_dense()
1623
for i, value in enumerate(arr):
1724
tm.assert_almost_equal(value, dense[i])
1825
tm.assert_almost_equal(arr[-i], dense[-i])
1926

20-
def test_getitem_arraylike_mask(self):
27+
def test_getitem_arraylike_mask(self, arr):
2128
arr = SparseArray([0, 1, 2])
2229
result = arr[[True, False, True]]
2330
expected = SparseArray([0, 2])
@@ -81,7 +88,7 @@ def test_boolean_slice_empty(self):
8188
res = arr[[False, False, False]]
8289
assert res.dtype == arr.dtype
8390

84-
def test_getitem_bool_sparse_array(self):
91+
def test_getitem_bool_sparse_array(self, arr):
8592
# GH 23122
8693
spar_bool = SparseArray([False, True] * 5, dtype=np.bool_, fill_value=True)
8794
exp = SparseArray([np.nan, 2, np.nan, 5, 6])
@@ -106,7 +113,7 @@ def test_getitem_bool_sparse_array_as_comparison(self):
106113
exp = SparseArray([3.0, 4.0], fill_value=np.nan)
107114
tm.assert_sp_array_equal(res, exp)
108115

109-
def test_get_item(self):
116+
def test_get_item(self, arr):
110117
zarr = SparseArray([0, 0, 1, 2, 3, 0, 4, 5, 0, 6], fill_value=0)
111118

112119
assert np.isnan(arr[1])
@@ -129,7 +136,7 @@ def test_get_item(self):
129136

130137

131138
class TestSetitem:
132-
def test_set_item(self):
139+
def test_set_item(self, arr_data):
133140
arr = SparseArray(arr_data).copy()
134141

135142
def setitem():
@@ -146,12 +153,12 @@ def setslice():
146153

147154

148155
class TestTake:
149-
def test_take_scalar_raises(self):
156+
def test_take_scalar_raises(self, arr):
150157
msg = "'indices' must be an array, not a scalar '2'."
151158
with pytest.raises(ValueError, match=msg):
152159
arr.take(2)
153160

154-
def test_take(self):
161+
def test_take(self, arr_data, arr):
155162
exp = SparseArray(np.take(arr_data, [2, 3]))
156163
tm.assert_sp_array_equal(arr.take([2, 3]), exp)
157164

@@ -173,14 +180,14 @@ def test_take_fill_value(self):
173180
exp = SparseArray(np.take(data, [1, 3, 4]), fill_value=0)
174181
tm.assert_sp_array_equal(sparse.take([1, 3, 4]), exp)
175182

176-
def test_take_negative(self):
183+
def test_take_negative(self, arr_data, arr):
177184
exp = SparseArray(np.take(arr_data, [-1]))
178185
tm.assert_sp_array_equal(arr.take([-1]), exp)
179186

180187
exp = SparseArray(np.take(arr_data, [-4, -3, -2]))
181188
tm.assert_sp_array_equal(arr.take([-4, -3, -2]), exp)
182189

183-
def test_bad_take(self):
190+
def test_bad_take(self, arr):
184191
with pytest.raises(IndexError, match="bounds"):
185192
arr.take([11])
186193

pandas/tests/arrays/sparse/test_libsparse.py

+87-90
Original file line numberDiff line numberDiff line change
@@ -14,77 +14,74 @@
1414
make_sparse_index,
1515
)
1616

17-
TEST_LENGTH = 20
18-
19-
plain_case = [
20-
[0, 7, 15],
21-
[3, 5, 5],
22-
[2, 9, 14],
23-
[2, 3, 5],
24-
[2, 9, 15],
25-
[1, 3, 4],
26-
]
27-
delete_blocks = [
28-
[0, 5],
29-
[4, 4],
30-
[1],
31-
[4],
32-
[1],
33-
[3],
34-
]
35-
split_blocks = [
36-
[0],
37-
[10],
38-
[0, 5],
39-
[3, 7],
40-
[0, 5],
41-
[3, 5],
42-
]
43-
skip_block = [
44-
[10],
45-
[5],
46-
[0, 12],
47-
[5, 3],
48-
[12],
49-
[3],
50-
]
51-
52-
no_intersect = [
53-
[0, 10],
54-
[4, 6],
55-
[5, 17],
56-
[4, 2],
57-
[],
58-
[],
59-
]
60-
61-
one_empty = [
62-
[0],
63-
[5],
64-
[],
65-
[],
66-
[],
67-
[],
68-
]
69-
70-
both_empty = [ # type: ignore[var-annotated]
71-
[],
72-
[],
73-
[],
74-
[],
75-
[],
76-
[],
77-
]
78-
79-
CASES = [plain_case, delete_blocks, split_blocks, skip_block, no_intersect, one_empty]
80-
IDS = [
81-
"plain_case",
82-
"delete_blocks",
83-
"split_blocks",
84-
"skip_block",
85-
"no_intersect",
86-
"one_empty",
87-
]
17+
18+
@pytest.fixture
19+
def test_length():
20+
return 20
21+
22+
23+
@pytest.fixture(
24+
params=[
25+
[
26+
[0, 7, 15],
27+
[3, 5, 5],
28+
[2, 9, 14],
29+
[2, 3, 5],
30+
[2, 9, 15],
31+
[1, 3, 4],
32+
],
33+
[
34+
[0, 5],
35+
[4, 4],
36+
[1],
37+
[4],
38+
[1],
39+
[3],
40+
],
41+
[
42+
[0],
43+
[10],
44+
[0, 5],
45+
[3, 7],
46+
[0, 5],
47+
[3, 5],
48+
],
49+
[
50+
[10],
51+
[5],
52+
[0, 12],
53+
[5, 3],
54+
[12],
55+
[3],
56+
],
57+
[
58+
[0, 10],
59+
[4, 6],
60+
[5, 17],
61+
[4, 2],
62+
[],
63+
[],
64+
],
65+
[
66+
[0],
67+
[5],
68+
[],
69+
[],
70+
[],
71+
[],
72+
],
73+
],
74+
ids=[
75+
"plain_case",
76+
"delete_blocks",
77+
"split_blocks",
78+
"skip_block",
79+
"no_intersect",
80+
"one_empty",
81+
],
82+
)
83+
def cases(request):
84+
return request.param
8885

8986

9087
class TestSparseIndexUnion:
@@ -101,7 +98,7 @@ class TestSparseIndexUnion:
10198
[[0, 10], [3, 3], [5, 15], [2, 2], [0, 5, 10, 15], [3, 2, 3, 2]],
10299
],
103100
)
104-
def test_index_make_union(self, xloc, xlen, yloc, ylen, eloc, elen):
101+
def test_index_make_union(self, xloc, xlen, yloc, ylen, eloc, elen, test_length):
105102
# Case 1
106103
# x: ----
107104
# y: ----
@@ -132,8 +129,8 @@ def test_index_make_union(self, xloc, xlen, yloc, ylen, eloc, elen):
132129
# Case 8
133130
# x: ---- ---
134131
# y: --- ---
135-
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
136-
yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
132+
xindex = BlockIndex(test_length, xloc, xlen)
133+
yindex = BlockIndex(test_length, yloc, ylen)
137134
bresult = xindex.make_union(yindex)
138135
assert isinstance(bresult, BlockIndex)
139136
tm.assert_numpy_array_equal(bresult.blocs, np.array(eloc, dtype=np.int32))
@@ -180,12 +177,12 @@ def test_int_index_make_union(self):
180177

181178
class TestSparseIndexIntersect:
182179
@td.skip_if_windows
183-
@pytest.mark.parametrize("xloc, xlen, yloc, ylen, eloc, elen", CASES, ids=IDS)
184-
def test_intersect(self, xloc, xlen, yloc, ylen, eloc, elen):
185-
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
186-
yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
187-
expected = BlockIndex(TEST_LENGTH, eloc, elen)
188-
longer_index = BlockIndex(TEST_LENGTH + 1, yloc, ylen)
180+
def test_intersect(self, cases, test_length):
181+
xloc, xlen, yloc, ylen, eloc, elen = cases
182+
xindex = BlockIndex(test_length, xloc, xlen)
183+
yindex = BlockIndex(test_length, yloc, ylen)
184+
expected = BlockIndex(test_length, eloc, elen)
185+
longer_index = BlockIndex(test_length + 1, yloc, ylen)
189186

190187
result = xindex.intersect(yindex)
191188
assert result.equals(expected)
@@ -493,10 +490,10 @@ def test_equals(self):
493490
assert index.equals(index)
494491
assert not index.equals(IntIndex(10, [0, 1, 2, 3]))
495492

496-
@pytest.mark.parametrize("xloc, xlen, yloc, ylen, eloc, elen", CASES, ids=IDS)
497-
def test_to_block_index(self, xloc, xlen, yloc, ylen, eloc, elen):
498-
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
499-
yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
493+
def test_to_block_index(self, cases, test_length):
494+
xloc, xlen, yloc, ylen, _, _ = cases
495+
xindex = BlockIndex(test_length, xloc, xlen)
496+
yindex = BlockIndex(test_length, yloc, ylen)
500497

501498
# see if survive the round trip
502499
xbindex = xindex.to_int_index().to_block_index()
@@ -512,13 +509,13 @@ def test_to_int_index(self):
512509

513510
class TestSparseOperators:
514511
@pytest.mark.parametrize("opname", ["add", "sub", "mul", "truediv", "floordiv"])
515-
@pytest.mark.parametrize("xloc, xlen, yloc, ylen, eloc, elen", CASES, ids=IDS)
516-
def test_op(self, opname, xloc, xlen, yloc, ylen, eloc, elen):
512+
def test_op(self, opname, cases, test_length):
513+
xloc, xlen, yloc, ylen, _, _ = cases
517514
sparse_op = getattr(splib, f"sparse_{opname}_float64")
518515
python_op = getattr(operator, opname)
519516

520-
xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
521-
yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
517+
xindex = BlockIndex(test_length, xloc, xlen)
518+
yindex = BlockIndex(test_length, yloc, ylen)
522519

523520
xdindex = xindex.to_int_index()
524521
ydindex = yindex.to_int_index()
@@ -542,10 +539,10 @@ def test_op(self, opname, xloc, xlen, yloc, ylen, eloc, elen):
542539

543540
# check versus Series...
544541
xseries = Series(x, xdindex.indices)
545-
xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill)
542+
xseries = xseries.reindex(np.arange(test_length)).fillna(xfill)
546543

547544
yseries = Series(y, ydindex.indices)
548-
yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill)
545+
yseries = yseries.reindex(np.arange(test_length)).fillna(yfill)
549546

550547
series_result = python_op(xseries, yseries)
551548
series_result = series_result.reindex(ri_index.indices)

0 commit comments

Comments
 (0)