Skip to content

Commit e9974d3

Browse files
mroeschkephofl
authored andcommitted
CLN: test_expressions.py (pandas-dev#49422)
1 parent 413d401 commit e9974d3

File tree

1 file changed

+112
-60
lines changed

1 file changed

+112
-60
lines changed

pandas/tests/test_expressions.py

+112-60
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,88 @@
1414
)
1515
from pandas.core.computation import expressions as expr
1616

17-
_frame = DataFrame(np.random.randn(10001, 4), columns=list("ABCD"), dtype="float64")
18-
_frame2 = DataFrame(np.random.randn(100, 4), columns=list("ABCD"), dtype="float64")
19-
_mixed = DataFrame(
20-
{
21-
"A": _frame["A"].copy(),
22-
"B": _frame["B"].astype("float32"),
23-
"C": _frame["C"].astype("int64"),
24-
"D": _frame["D"].astype("int32"),
25-
}
26-
)
27-
_mixed2 = DataFrame(
28-
{
29-
"A": _frame2["A"].copy(),
30-
"B": _frame2["B"].astype("float32"),
31-
"C": _frame2["C"].astype("int64"),
32-
"D": _frame2["D"].astype("int32"),
33-
}
34-
)
35-
_integer = DataFrame(
36-
np.random.randint(1, 100, size=(10001, 4)), columns=list("ABCD"), dtype="int64"
37-
)
38-
_integer2 = DataFrame(
39-
np.random.randint(1, 100, size=(101, 4)), columns=list("ABCD"), dtype="int64"
40-
)
41-
_array = _frame["A"].values.copy()
42-
_array2 = _frame2["A"].values.copy()
4317

44-
_array_mixed = _mixed["D"].values.copy()
45-
_array_mixed2 = _mixed2["D"].values.copy()
18+
@pytest.fixture
19+
def _frame():
20+
return DataFrame(np.random.randn(10001, 4), columns=list("ABCD"), dtype="float64")
21+
22+
23+
@pytest.fixture
24+
def _frame2():
25+
return DataFrame(np.random.randn(100, 4), columns=list("ABCD"), dtype="float64")
26+
27+
28+
@pytest.fixture
29+
def _mixed(_frame):
30+
return DataFrame(
31+
{
32+
"A": _frame["A"].copy(),
33+
"B": _frame["B"].astype("float32"),
34+
"C": _frame["C"].astype("int64"),
35+
"D": _frame["D"].astype("int32"),
36+
}
37+
)
38+
39+
40+
@pytest.fixture
41+
def _mixed2(_frame2):
42+
return DataFrame(
43+
{
44+
"A": _frame2["A"].copy(),
45+
"B": _frame2["B"].astype("float32"),
46+
"C": _frame2["C"].astype("int64"),
47+
"D": _frame2["D"].astype("int32"),
48+
}
49+
)
50+
51+
52+
@pytest.fixture
53+
def _integer():
54+
return DataFrame(
55+
np.random.randint(1, 100, size=(10001, 4)), columns=list("ABCD"), dtype="int64"
56+
)
57+
58+
59+
@pytest.fixture
60+
def _integer_randint(_integer):
61+
# randint to get a case with zeros
62+
return _integer * np.random.randint(0, 2, size=np.shape(_integer))
63+
64+
65+
@pytest.fixture
66+
def _integer2():
67+
return DataFrame(
68+
np.random.randint(1, 100, size=(101, 4)), columns=list("ABCD"), dtype="int64"
69+
)
70+
71+
72+
@pytest.fixture
73+
def _array(_frame):
74+
return _frame["A"].values.copy()
75+
76+
77+
@pytest.fixture
78+
def _array2(_frame2):
79+
return _frame2["A"].values.copy()
80+
81+
82+
@pytest.fixture
83+
def _array_mixed(_mixed):
84+
return _mixed["D"].values.copy()
85+
86+
87+
@pytest.fixture
88+
def _array_mixed2(_mixed2):
89+
return _mixed2["D"].values.copy()
4690

4791

4892
@pytest.mark.skipif(not expr.USE_NUMEXPR, reason="not using numexpr")
4993
class TestExpressions:
50-
def setup_method(self):
51-
self._MIN_ELEMENTS = expr._MIN_ELEMENTS
52-
53-
def teardown_method(self):
54-
expr._MIN_ELEMENTS = self._MIN_ELEMENTS
94+
@pytest.fixture(autouse=True)
95+
def save_min_elements(self):
96+
min_elements = expr._MIN_ELEMENTS
97+
yield
98+
expr._MIN_ELEMENTS = min_elements
5599

56100
@staticmethod
57101
def call_op(df, other, flex: bool, opname: str):
@@ -70,23 +114,23 @@ def call_op(df, other, flex: bool, opname: str):
70114
return result, expected
71115

72116
@pytest.mark.parametrize(
73-
"df",
117+
"fixture",
74118
[
75-
_integer,
76-
_integer2,
77-
# randint to get a case with zeros
78-
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
79-
_frame,
80-
_frame2,
81-
_mixed,
82-
_mixed2,
119+
"_integer",
120+
"_integer2",
121+
"_integer_randint",
122+
"_frame",
123+
"_frame2",
124+
"_mixed",
125+
"_mixed2",
83126
],
84127
)
85128
@pytest.mark.parametrize("flex", [True, False])
86129
@pytest.mark.parametrize(
87130
"arith", ["add", "sub", "mul", "mod", "truediv", "floordiv"]
88131
)
89-
def test_run_arithmetic(self, df, flex, arith):
132+
def test_run_arithmetic(self, request, fixture, flex, arith):
133+
df = request.getfixturevalue(fixture)
90134
expr._MIN_ELEMENTS = 0
91135
result, expected = self.call_op(df, df, flex, arith)
92136

@@ -101,25 +145,25 @@ def test_run_arithmetic(self, df, flex, arith):
101145
tm.assert_equal(expected, result)
102146

103147
@pytest.mark.parametrize(
104-
"df",
148+
"fixture",
105149
[
106-
_integer,
107-
_integer2,
108-
# randint to get a case with zeros
109-
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
110-
_frame,
111-
_frame2,
112-
_mixed,
113-
_mixed2,
150+
"_integer",
151+
"_integer2",
152+
"_integer_randint",
153+
"_frame",
154+
"_frame2",
155+
"_mixed",
156+
"_mixed2",
114157
],
115158
)
116159
@pytest.mark.parametrize("flex", [True, False])
117-
def test_run_binary(self, df, flex, comparison_op):
160+
def test_run_binary(self, request, fixture, flex, comparison_op):
118161
"""
119162
tests solely that the result is the same whether or not numexpr is
120163
enabled. Need to test whether the function does the correct thing
121164
elsewhere.
122165
"""
166+
df = request.getfixturevalue(fixture)
123167
arith = comparison_op.__name__
124168
with option_context("compute.use_numexpr", False):
125169
other = df.copy() + 1
@@ -160,9 +204,12 @@ def test_invalid(self):
160204
[("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"), ("pow", "**")],
161205
)
162206
@pytest.mark.parametrize(
163-
"left,right", [(_array, _array2), (_array_mixed, _array_mixed2)]
207+
"left_fix,right_fix", [("_array", "_array2"), ("_array_mixed", "_array_mixed2")]
164208
)
165-
def test_binary_ops(self, opname, op_str, left, right):
209+
def test_binary_ops(self, request, opname, op_str, left_fix, right_fix):
210+
left = request.getfixturevalue(left_fix)
211+
right = request.getfixturevalue(right_fix)
212+
166213
def testit():
167214

168215
if opname == "pow":
@@ -202,9 +249,12 @@ def testit():
202249
],
203250
)
204251
@pytest.mark.parametrize(
205-
"left,right", [(_array, _array2), (_array_mixed, _array_mixed2)]
252+
"left_fix,right_fix", [("_array", "_array2"), ("_array_mixed", "_array_mixed2")]
206253
)
207-
def test_comparison_ops(self, opname, op_str, left, right):
254+
def test_comparison_ops(self, request, opname, op_str, left_fix, right_fix):
255+
left = request.getfixturevalue(left_fix)
256+
right = request.getfixturevalue(right_fix)
257+
208258
def testit():
209259
f12 = left + 1
210260
f22 = right + 1
@@ -227,8 +277,10 @@ def testit():
227277
testit()
228278

229279
@pytest.mark.parametrize("cond", [True, False])
230-
@pytest.mark.parametrize("df", [_frame, _frame2, _mixed, _mixed2])
231-
def test_where(self, cond, df):
280+
@pytest.mark.parametrize("fixture", ["_frame", "_frame2", "_mixed", "_mixed2"])
281+
def test_where(self, request, cond, fixture):
282+
df = request.getfixturevalue(fixture)
283+
232284
def testit():
233285
c = np.empty(df.shape, dtype=np.bool_)
234286
c.fill(cond)
@@ -350,7 +402,7 @@ def test_bool_ops_column_name_dtype(self, test_input, expected):
350402
"arith", ("add", "sub", "mul", "mod", "truediv", "floordiv")
351403
)
352404
@pytest.mark.parametrize("axis", (0, 1))
353-
def test_frame_series_axis(self, axis, arith):
405+
def test_frame_series_axis(self, axis, arith, _frame):
354406
# GH#26736 Dataframe.floordiv(Series, axis=1) fails
355407

356408
df = _frame

0 commit comments

Comments
 (0)