Skip to content

Commit 1810631

Browse files
authored
TST: Parameterize & split (#45276)
1 parent d603d43 commit 1810631

File tree

4 files changed

+170
-151
lines changed

4 files changed

+170
-151
lines changed

pandas/tests/computation/test_eval.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -666,20 +666,25 @@ def test_float_comparison_bin_op(self, dtype):
666666
res = df.eval("-5 > x")
667667
assert res.values == np.array([False])
668668

669-
def test_disallow_scalar_bool_ops(self):
670-
exprs = "1 or 2", "1 and 2"
671-
exprs += "a and b", "a or b"
672-
exprs += ("1 or 2 and (3 + 2) > 3",)
673-
exprs += ("2 * x > 2 or 1 and 2",)
674-
exprs += ("2 * df > 3 and 1 or a",)
675-
669+
@pytest.mark.parametrize(
670+
"ex",
671+
(
672+
"1 or 2",
673+
"1 and 2",
674+
"a and b",
675+
"a or b",
676+
"1 or 2 and (3 + 2) > 3",
677+
"2 * x > 2 or 1 and 2",
678+
"2 * df > 3 and 1 or a",
679+
),
680+
)
681+
def test_disallow_scalar_bool_ops(self, ex):
676682
x, a, b = np.random.randn(3), 1, 2 # noqa:F841
677683
df = DataFrame(np.random.randn(3, 2)) # noqa:F841
678684

679-
for ex in exprs:
680-
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
681-
with pytest.raises(NotImplementedError, match=msg):
682-
pd.eval(ex, engine=self.engine, parser=self.parser)
685+
msg = "cannot evaluate scalar only bool ops|'BoolOp' nodes are not"
686+
with pytest.raises(NotImplementedError, match=msg):
687+
pd.eval(ex, engine=self.engine, parser=self.parser)
683688

684689
def test_identical(self):
685690
# see gh-10546

pandas/tests/libs/test_hashtable.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,19 @@ def test_get_state(self, table_type, dtype):
165165
assert "n_buckets" in state
166166
assert "upper_bound" in state
167167

168-
def test_no_reallocation(self, table_type, dtype):
169-
for N in range(1, 110):
170-
keys = np.arange(N).astype(dtype)
171-
preallocated_table = table_type(N)
172-
n_buckets_start = preallocated_table.get_state()["n_buckets"]
173-
preallocated_table.map_locations(keys)
174-
n_buckets_end = preallocated_table.get_state()["n_buckets"]
175-
# original number of buckets was enough:
176-
assert n_buckets_start == n_buckets_end
177-
# check with clean table (not too much preallocated)
178-
clean_table = table_type()
179-
clean_table.map_locations(keys)
180-
assert n_buckets_start == clean_table.get_state()["n_buckets"]
168+
@pytest.mark.parametrize("N", range(1, 110))
169+
def test_no_reallocation(self, table_type, dtype, N):
170+
keys = np.arange(N).astype(dtype)
171+
preallocated_table = table_type(N)
172+
n_buckets_start = preallocated_table.get_state()["n_buckets"]
173+
preallocated_table.map_locations(keys)
174+
n_buckets_end = preallocated_table.get_state()["n_buckets"]
175+
# original number of buckets was enough:
176+
assert n_buckets_start == n_buckets_end
177+
# check with clean table (not too much preallocated)
178+
clean_table = table_type()
179+
clean_table.map_locations(keys)
180+
assert n_buckets_start == clean_table.get_state()["n_buckets"]
181181

182182

183183
class TestPyObjectHashTableWithNans:
@@ -282,19 +282,19 @@ def test_tracemalloc_for_empty_StringHashTable():
282282
assert get_allocated_khash_memory() == 0
283283

284284

285-
def test_no_reallocation_StringHashTable():
286-
for N in range(1, 110):
287-
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
288-
preallocated_table = ht.StringHashTable(N)
289-
n_buckets_start = preallocated_table.get_state()["n_buckets"]
290-
preallocated_table.map_locations(keys)
291-
n_buckets_end = preallocated_table.get_state()["n_buckets"]
292-
# original number of buckets was enough:
293-
assert n_buckets_start == n_buckets_end
294-
# check with clean table (not too much preallocated)
295-
clean_table = ht.StringHashTable()
296-
clean_table.map_locations(keys)
297-
assert n_buckets_start == clean_table.get_state()["n_buckets"]
285+
@pytest.mark.parametrize("N", range(1, 110))
286+
def test_no_reallocation_StringHashTable(N):
287+
keys = np.arange(N).astype(np.compat.unicode).astype(np.object_)
288+
preallocated_table = ht.StringHashTable(N)
289+
n_buckets_start = preallocated_table.get_state()["n_buckets"]
290+
preallocated_table.map_locations(keys)
291+
n_buckets_end = preallocated_table.get_state()["n_buckets"]
292+
# original number of buckets was enough:
293+
assert n_buckets_start == n_buckets_end
294+
# check with clean table (not too much preallocated)
295+
clean_table = ht.StringHashTable()
296+
clean_table.map_locations(keys)
297+
assert n_buckets_start == clean_table.get_state()["n_buckets"]
298298

299299

300300
@pytest.mark.parametrize(

pandas/tests/libs/test_lib.py

Lines changed: 90 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -54,50 +54,58 @@ def test_maybe_indices_to_slice_left_edge(self):
5454
assert isinstance(maybe_slice, slice)
5555
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
5656

57-
for end in [1, 2, 5, 20, 99]:
58-
for step in [1, 2, 4]:
59-
indices = np.arange(0, end, step, dtype=np.intp)
60-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
57+
@pytest.mark.parametrize("end", [1, 2, 5, 20, 99])
58+
@pytest.mark.parametrize("step", [1, 2, 4])
59+
def test_maybe_indices_to_slice_left_edge_not_slice_end_steps(self, end, step):
60+
target = np.arange(100)
61+
indices = np.arange(0, end, step, dtype=np.intp)
62+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
6163

62-
assert isinstance(maybe_slice, slice)
63-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
64+
assert isinstance(maybe_slice, slice)
65+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
6466

65-
# reverse
66-
indices = indices[::-1]
67-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
67+
# reverse
68+
indices = indices[::-1]
69+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
6870

69-
assert isinstance(maybe_slice, slice)
70-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
71+
assert isinstance(maybe_slice, slice)
72+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
7173

74+
@pytest.mark.parametrize(
75+
"case", [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]
76+
)
77+
def test_maybe_indices_to_slice_left_edge_not_slice(self, case):
7278
# not slice
73-
for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2], [2, 0, -2]]:
74-
indices = np.array(case, dtype=np.intp)
75-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
79+
target = np.arange(100)
80+
indices = np.array(case, dtype=np.intp)
81+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
7682

77-
assert not isinstance(maybe_slice, slice)
78-
tm.assert_numpy_array_equal(maybe_slice, indices)
79-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
83+
assert not isinstance(maybe_slice, slice)
84+
tm.assert_numpy_array_equal(maybe_slice, indices)
85+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
8086

81-
def test_maybe_indices_to_slice_right_edge(self):
87+
@pytest.mark.parametrize("start", [0, 2, 5, 20, 97, 98])
88+
@pytest.mark.parametrize("step", [1, 2, 4])
89+
def test_maybe_indices_to_slice_right_edge(self, start, step):
8290
target = np.arange(100)
8391

8492
# slice
85-
for start in [0, 2, 5, 20, 97, 98]:
86-
for step in [1, 2, 4]:
87-
indices = np.arange(start, 99, step, dtype=np.intp)
88-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
93+
indices = np.arange(start, 99, step, dtype=np.intp)
94+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
8995

90-
assert isinstance(maybe_slice, slice)
91-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
96+
assert isinstance(maybe_slice, slice)
97+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
9298

93-
# reverse
94-
indices = indices[::-1]
95-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
99+
# reverse
100+
indices = indices[::-1]
101+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
96102

97-
assert isinstance(maybe_slice, slice)
98-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
103+
assert isinstance(maybe_slice, slice)
104+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
99105

106+
def test_maybe_indices_to_slice_right_edge_not_slice(self):
100107
# not slice
108+
target = np.arange(100)
101109
indices = np.array([97, 98, 99, 100], dtype=np.intp)
102110
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
103111

@@ -122,65 +130,75 @@ def test_maybe_indices_to_slice_right_edge(self):
122130
with pytest.raises(IndexError, match=msg):
123131
target[maybe_slice]
124132

125-
for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
126-
indices = np.array(case, dtype=np.intp)
127-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
133+
@pytest.mark.parametrize(
134+
"case", [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]
135+
)
136+
def test_maybe_indices_to_slice_right_edge_cases(self, case):
137+
target = np.arange(100)
138+
indices = np.array(case, dtype=np.intp)
139+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
128140

129-
assert not isinstance(maybe_slice, slice)
130-
tm.assert_numpy_array_equal(maybe_slice, indices)
131-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
141+
assert not isinstance(maybe_slice, slice)
142+
tm.assert_numpy_array_equal(maybe_slice, indices)
143+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
132144

133-
def test_maybe_indices_to_slice_both_edges(self):
145+
@pytest.mark.parametrize("step", [1, 2, 4, 5, 8, 9])
146+
def test_maybe_indices_to_slice_both_edges(self, step):
134147
target = np.arange(10)
135148

136149
# slice
137-
for step in [1, 2, 4, 5, 8, 9]:
138-
indices = np.arange(0, 9, step, dtype=np.intp)
139-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
140-
assert isinstance(maybe_slice, slice)
141-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
142-
143-
# reverse
144-
indices = indices[::-1]
145-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
146-
assert isinstance(maybe_slice, slice)
147-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
150+
indices = np.arange(0, 9, step, dtype=np.intp)
151+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
152+
assert isinstance(maybe_slice, slice)
153+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
154+
155+
# reverse
156+
indices = indices[::-1]
157+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
158+
assert isinstance(maybe_slice, slice)
159+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
148160

161+
@pytest.mark.parametrize("case", [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]])
162+
def test_maybe_indices_to_slice_both_edges_not_slice(self, case):
149163
# not slice
150-
for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
151-
indices = np.array(case, dtype=np.intp)
152-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
153-
assert not isinstance(maybe_slice, slice)
154-
tm.assert_numpy_array_equal(maybe_slice, indices)
155-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
156-
157-
def test_maybe_indices_to_slice_middle(self):
164+
target = np.arange(10)
165+
indices = np.array(case, dtype=np.intp)
166+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
167+
assert not isinstance(maybe_slice, slice)
168+
tm.assert_numpy_array_equal(maybe_slice, indices)
169+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
170+
171+
@pytest.mark.parametrize("start, end", [(2, 10), (5, 25), (65, 97)])
172+
@pytest.mark.parametrize("step", [1, 2, 4, 20])
173+
def test_maybe_indices_to_slice_middle(self, start, end, step):
158174
target = np.arange(100)
159175

160176
# slice
161-
for start, end in [(2, 10), (5, 25), (65, 97)]:
162-
for step in [1, 2, 4, 20]:
163-
indices = np.arange(start, end, step, dtype=np.intp)
164-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
177+
indices = np.arange(start, end, step, dtype=np.intp)
178+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
165179

166-
assert isinstance(maybe_slice, slice)
167-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
180+
assert isinstance(maybe_slice, slice)
181+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
168182

169-
# reverse
170-
indices = indices[::-1]
171-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
183+
# reverse
184+
indices = indices[::-1]
185+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
172186

173-
assert isinstance(maybe_slice, slice)
174-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
187+
assert isinstance(maybe_slice, slice)
188+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
175189

190+
@pytest.mark.parametrize(
191+
"case", [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]
192+
)
193+
def test_maybe_indices_to_slice_middle_not_slice(self, case):
176194
# not slice
177-
for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
178-
indices = np.array(case, dtype=np.intp)
179-
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
195+
target = np.arange(100)
196+
indices = np.array(case, dtype=np.intp)
197+
maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
180198

181-
assert not isinstance(maybe_slice, slice)
182-
tm.assert_numpy_array_equal(maybe_slice, indices)
183-
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
199+
assert not isinstance(maybe_slice, slice)
200+
tm.assert_numpy_array_equal(maybe_slice, indices)
201+
tm.assert_numpy_array_equal(target[indices], target[maybe_slice])
184202

185203
def test_maybe_booleans_to_slice(self):
186204
arr = np.array([0, 0, 1, 1, 1, 0, 1], dtype=np.uint8)

0 commit comments

Comments
 (0)