Skip to content

Commit 1b91eeb

Browse files
jbrockmendelproost
authored andcommitted
TST: parametrize test_expressions (pandas-dev#28493)
1 parent 3e3aa8d commit 1b91eeb

File tree

2 files changed

+80
-149
lines changed

2 files changed

+80
-149
lines changed

pandas/tests/io/excel/test_xlsxwriter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def test_column_format(ext):
5050

5151
try:
5252
read_num_format = cell.number_format
53-
except Exception:
53+
except AttributeError:
5454
read_num_format = cell.style.number_format._format_code
5555

5656
assert read_num_format == num_format

pandas/tests/test_expressions.py

+79-148
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@
88
from pandas.core.api import DataFrame
99
from pandas.core.computation import expressions as expr
1010
import pandas.util.testing as tm
11-
from pandas.util.testing import (
12-
assert_almost_equal,
13-
assert_frame_equal,
14-
assert_series_equal,
15-
)
16-
17-
from pandas.io.formats.printing import pprint_thing
11+
from pandas.util.testing import assert_frame_equal
1812

1913
_frame = DataFrame(randn(10000, 4), columns=list("ABCD"), dtype="float64")
2014
_frame2 = DataFrame(randn(100, 4), columns=list("ABCD"), dtype="float64")
@@ -50,57 +44,37 @@ def setup_method(self, method):
5044
self.frame2 = _frame2.copy()
5145
self.mixed = _mixed.copy()
5246
self.mixed2 = _mixed2.copy()
53-
self.integer = _integer.copy()
5447
self._MIN_ELEMENTS = expr._MIN_ELEMENTS
5548

5649
def teardown_method(self, method):
5750
expr._MIN_ELEMENTS = self._MIN_ELEMENTS
5851

59-
def run_arithmetic(self, df, other, assert_func, check_dtype=False, test_flex=True):
52+
def run_arithmetic(self, df, other):
6053
expr._MIN_ELEMENTS = 0
6154
operations = ["add", "sub", "mul", "mod", "truediv", "floordiv"]
62-
for arith in operations:
55+
for test_flex in [True, False]:
56+
for arith in operations:
6357

64-
operator_name = arith
65-
if arith == "div":
66-
operator_name = "truediv"
58+
operator_name = arith
6759

68-
if test_flex:
69-
op = lambda x, y: getattr(x, arith)(y)
70-
op.__name__ = arith
71-
else:
72-
op = getattr(operator, operator_name)
73-
expr.set_use_numexpr(False)
74-
expected = op(df, other)
75-
expr.set_use_numexpr(True)
60+
if test_flex:
61+
op = lambda x, y: getattr(x, arith)(y)
62+
op.__name__ = arith
63+
else:
64+
op = getattr(operator, operator_name)
65+
expr.set_use_numexpr(False)
66+
expected = op(df, other)
67+
expr.set_use_numexpr(True)
7668

77-
result = op(df, other)
78-
try:
79-
if check_dtype:
80-
if arith == "truediv":
69+
result = op(df, other)
70+
if arith == "truediv":
71+
if expected.ndim == 1:
8172
assert expected.dtype.kind == "f"
82-
assert_func(expected, result)
83-
except Exception:
84-
pprint_thing("Failed test with operator {op.__name__!r}".format(op=op))
85-
raise
86-
87-
def test_integer_arithmetic(self):
88-
self.run_arithmetic(self.integer, self.integer, assert_frame_equal)
89-
self.run_arithmetic(
90-
self.integer.iloc[:, 0],
91-
self.integer.iloc[:, 0],
92-
assert_series_equal,
93-
check_dtype=True,
94-
)
73+
else:
74+
assert all(x.kind == "f" for x in expected.dtypes.values)
75+
tm.assert_equal(expected, result)
9576

96-
def run_binary(
97-
self,
98-
df,
99-
other,
100-
assert_func,
101-
test_flex=False,
102-
numexpr_ops={"gt", "lt", "ge", "le", "eq", "ne"},
103-
):
77+
def run_binary(self, df, other):
10478
"""
10579
tests solely that the result is the same whether or not numexpr is
10680
enabled. Need to test whether the function does the correct thing
@@ -110,98 +84,58 @@ def run_binary(
11084
expr.set_test_mode(True)
11185
operations = ["gt", "lt", "ge", "le", "eq", "ne"]
11286

113-
for arith in operations:
114-
if test_flex:
115-
op = lambda x, y: getattr(df, arith)(y)
116-
op.__name__ = arith
117-
else:
118-
op = getattr(operator, arith)
119-
expr.set_use_numexpr(False)
120-
expected = op(df, other)
121-
expr.set_use_numexpr(True)
122-
expr.get_test_result()
123-
result = op(df, other)
124-
used_numexpr = expr.get_test_result()
125-
try:
126-
if arith in numexpr_ops:
127-
assert used_numexpr, "Did not use numexpr as expected."
87+
for test_flex in [True, False]:
88+
for arith in operations:
89+
if test_flex:
90+
op = lambda x, y: getattr(df, arith)(y)
91+
op.__name__ = arith
12892
else:
129-
assert not used_numexpr, "Used numexpr unexpectedly."
130-
assert_func(expected, result)
131-
except Exception:
132-
pprint_thing("Failed test with operation {arith!r}".format(arith=arith))
133-
pprint_thing("test_flex was {test_flex!r}".format(test_flex=test_flex))
134-
raise
135-
136-
def run_frame(self, df, other, binary_comp=None, run_binary=True, **kwargs):
137-
self.run_arithmetic(df, other, assert_frame_equal, test_flex=False, **kwargs)
138-
self.run_arithmetic(df, other, assert_frame_equal, test_flex=True, **kwargs)
139-
if run_binary:
140-
if binary_comp is None:
93+
op = getattr(operator, arith)
14194
expr.set_use_numexpr(False)
142-
binary_comp = other + 1
95+
expected = op(df, other)
14396
expr.set_use_numexpr(True)
144-
self.run_binary(
145-
df, binary_comp, assert_frame_equal, test_flex=False, **kwargs
146-
)
147-
self.run_binary(
148-
df, binary_comp, assert_frame_equal, test_flex=True, **kwargs
149-
)
150-
151-
def run_series(self, ser, other, binary_comp=None, **kwargs):
152-
self.run_arithmetic(ser, other, assert_series_equal, test_flex=False, **kwargs)
153-
self.run_arithmetic(ser, other, assert_almost_equal, test_flex=True, **kwargs)
154-
# FIXME: dont leave commented-out
155-
# series doesn't uses vec_compare instead of numexpr...
156-
# if binary_comp is None:
157-
# binary_comp = other + 1
158-
# self.run_binary(ser, binary_comp, assert_frame_equal,
159-
# test_flex=False, **kwargs)
160-
# self.run_binary(ser, binary_comp, assert_frame_equal,
161-
# test_flex=True, **kwargs)
162-
163-
def test_integer_arithmetic_frame(self):
164-
self.run_frame(self.integer, self.integer)
165-
166-
def test_integer_arithmetic_series(self):
167-
self.run_series(self.integer.iloc[:, 0], self.integer.iloc[:, 0])
168-
169-
def test_float_arithemtic_frame(self):
170-
self.run_frame(self.frame2, self.frame2)
171-
172-
def test_float_arithmetic_series(self):
173-
self.run_series(self.frame2.iloc[:, 0], self.frame2.iloc[:, 0])
174-
175-
def test_mixed_arithmetic_frame(self):
176-
# TODO: FIGURE OUT HOW TO GET IT TO WORK...
97+
expr.get_test_result()
98+
result = op(df, other)
99+
used_numexpr = expr.get_test_result()
100+
assert used_numexpr, "Did not use numexpr as expected."
101+
tm.assert_equal(expected, result)
102+
103+
def run_frame(self, df, other, run_binary=True):
104+
self.run_arithmetic(df, other)
105+
if run_binary:
106+
expr.set_use_numexpr(False)
107+
binary_comp = other + 1
108+
expr.set_use_numexpr(True)
109+
self.run_binary(df, binary_comp)
110+
111+
for i in range(len(df.columns)):
112+
self.run_arithmetic(df.iloc[:, i], other.iloc[:, i])
113+
# FIXME: dont leave commented-out
114+
# series doesn't uses vec_compare instead of numexpr...
115+
# binary_comp = other.iloc[:, i] + 1
116+
# self.run_binary(df.iloc[:, i], binary_comp)
117+
118+
@pytest.mark.parametrize(
119+
"df",
120+
[
121+
_integer,
122+
_integer2,
123+
# randint to get a case with zeros
124+
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
125+
_frame,
126+
_frame2,
127+
_mixed,
128+
_mixed2,
129+
],
130+
)
131+
def test_arithmetic(self, df):
132+
# TODO: FIGURE OUT HOW TO GET RUN_BINARY TO WORK WITH MIXED=...
177133
# can't do arithmetic because comparison methods try to do *entire*
178134
# frame instead of by-column
179-
self.run_frame(self.mixed2, self.mixed2, run_binary=False)
180-
181-
def test_mixed_arithmetic_series(self):
182-
for col in self.mixed2.columns:
183-
self.run_series(self.mixed2[col], self.mixed2[col], binary_comp=4)
184-
185-
def test_float_arithemtic(self):
186-
self.run_arithmetic(self.frame, self.frame, assert_frame_equal)
187-
self.run_arithmetic(
188-
self.frame.iloc[:, 0],
189-
self.frame.iloc[:, 0],
190-
assert_series_equal,
191-
check_dtype=True,
192-
)
193-
194-
def test_mixed_arithmetic(self):
195-
self.run_arithmetic(self.mixed, self.mixed, assert_frame_equal)
196-
for col in self.mixed.columns:
197-
self.run_arithmetic(self.mixed[col], self.mixed[col], assert_series_equal)
135+
kinds = {x.kind for x in df.dtypes.values}
136+
should = len(kinds) == 1
198137

199-
def test_integer_with_zeros(self):
200-
self.integer *= np.random.randint(0, 2, size=np.shape(self.integer))
201-
self.run_arithmetic(self.integer, self.integer, assert_frame_equal)
202-
self.run_arithmetic(
203-
self.integer.iloc[:, 0], self.integer.iloc[:, 0], assert_series_equal
204-
)
138+
self.run_frame(df, df, run_binary=should)
205139

206140
def test_invalid(self):
207141

@@ -231,7 +165,7 @@ def test_invalid(self):
231165

232166
@pytest.mark.parametrize(
233167
"opname,op_str",
234-
[("add", "+"), ("sub", "-"), ("mul", "*"), ("div", "/"), ("pow", "**")],
168+
[("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"), ("pow", "**")],
235169
)
236170
def test_binary_ops(self, opname, op_str):
237171
def testit():
@@ -241,24 +175,21 @@ def testit():
241175
if opname == "pow":
242176
continue
243177

244-
if opname == "div":
245-
op = getattr(operator, "truediv", None)
246-
else:
247-
op = getattr(operator, opname, None)
248-
if op is not None:
249-
result = expr._can_use_numexpr(op, op_str, f, f, "evaluate")
250-
assert result != f._is_mixed_type
178+
op = getattr(operator, opname)
251179

252-
result = expr.evaluate(op, op_str, f, f, use_numexpr=True)
253-
expected = expr.evaluate(op, op_str, f, f, use_numexpr=False)
180+
result = expr._can_use_numexpr(op, op_str, f, f, "evaluate")
181+
assert result != f._is_mixed_type
254182

255-
if isinstance(result, DataFrame):
256-
tm.assert_frame_equal(result, expected)
257-
else:
258-
tm.assert_numpy_array_equal(result, expected.values)
183+
result = expr.evaluate(op, op_str, f, f, use_numexpr=True)
184+
expected = expr.evaluate(op, op_str, f, f, use_numexpr=False)
185+
186+
if isinstance(result, DataFrame):
187+
tm.assert_frame_equal(result, expected)
188+
else:
189+
tm.assert_numpy_array_equal(result, expected.values)
259190

260-
result = expr._can_use_numexpr(op, op_str, f2, f2, "evaluate")
261-
assert not result
191+
result = expr._can_use_numexpr(op, op_str, f2, f2, "evaluate")
192+
assert not result
262193

263194
expr.set_use_numexpr(False)
264195
testit()

0 commit comments

Comments
 (0)