Skip to content

Commit f49907f

Browse files
authored
TST: parametrize eval tests (#31901)
1 parent f1c5cb0 commit f49907f

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

pandas/tests/frame/test_query_eval.py

+38-38
Original file line numberDiff line numberDiff line change
@@ -78,45 +78,48 @@ def test_query_numexpr(self):
7878

7979

8080
class TestDataFrameEval:
81-
def test_ops(self):
81+
82+
# smaller hits python, larger hits numexpr
83+
@pytest.mark.parametrize("n", [4, 4000])
84+
@pytest.mark.parametrize(
85+
"op_str,op,rop",
86+
[
87+
("+", "__add__", "__radd__"),
88+
("-", "__sub__", "__rsub__"),
89+
("*", "__mul__", "__rmul__"),
90+
("/", "__truediv__", "__rtruediv__"),
91+
],
92+
)
93+
def test_ops(self, op_str, op, rop, n):
8294

8395
# tst ops and reversed ops in evaluation
8496
# GH7198
8597

86-
# smaller hits python, larger hits numexpr
87-
for n in [4, 4000]:
88-
89-
df = DataFrame(1, index=range(n), columns=list("abcd"))
90-
df.iloc[0] = 2
91-
m = df.mean()
98+
df = DataFrame(1, index=range(n), columns=list("abcd"))
99+
df.iloc[0] = 2
100+
m = df.mean()
92101

93-
for op_str, op, rop in [
94-
("+", "__add__", "__radd__"),
95-
("-", "__sub__", "__rsub__"),
96-
("*", "__mul__", "__rmul__"),
97-
("/", "__truediv__", "__rtruediv__"),
98-
]:
99-
100-
base = DataFrame( # noqa
101-
np.tile(m.values, n).reshape(n, -1), columns=list("abcd")
102-
)
102+
base = DataFrame( # noqa
103+
np.tile(m.values, n).reshape(n, -1), columns=list("abcd")
104+
)
103105

104-
expected = eval("base{op}df".format(op=op_str))
106+
expected = eval(f"base {op_str} df")
105107

106-
# ops as strings
107-
result = eval("m{op}df".format(op=op_str))
108-
tm.assert_frame_equal(result, expected)
108+
# ops as strings
109+
result = eval(f"m {op_str} df")
110+
tm.assert_frame_equal(result, expected)
109111

110-
# these are commutative
111-
if op in ["+", "*"]:
112-
result = getattr(df, op)(m)
113-
tm.assert_frame_equal(result, expected)
112+
# these are commutative
113+
if op in ["+", "*"]:
114+
result = getattr(df, op)(m)
115+
tm.assert_frame_equal(result, expected)
114116

115-
# these are not
116-
elif op in ["-", "/"]:
117-
result = getattr(df, rop)(m)
118-
tm.assert_frame_equal(result, expected)
117+
# these are not
118+
elif op in ["-", "/"]:
119+
result = getattr(df, rop)(m)
120+
tm.assert_frame_equal(result, expected)
119121

122+
def test_dataframe_sub_numexpr_path(self):
120123
# GH7192: Note we need a large number of rows to ensure this
121124
# goes through the numexpr path
122125
df = DataFrame(dict(A=np.random.randn(25000)))
@@ -451,9 +454,7 @@ def test_date_query_with_non_date(self):
451454

452455
for op in ["<", ">", "<=", ">="]:
453456
with pytest.raises(TypeError):
454-
df.query(
455-
"dates {op} nondate".format(op=op), parser=parser, engine=engine
456-
)
457+
df.query(f"dates {op} nondate", parser=parser, engine=engine)
457458

458459
def test_query_syntax_error(self):
459460
engine, parser = self.engine, self.parser
@@ -687,10 +688,9 @@ def test_inf(self):
687688
n = 10
688689
df = DataFrame({"a": np.random.rand(n), "b": np.random.rand(n)})
689690
df.loc[::2, 0] = np.inf
690-
ops = "==", "!="
691-
d = dict(zip(ops, (operator.eq, operator.ne)))
691+
d = {"==": operator.eq, "!=": operator.ne}
692692
for op, f in d.items():
693-
q = "a {op} inf".format(op=op)
693+
q = f"a {op} inf"
694694
expected = df[f(df.a, np.inf)]
695695
result = df.query(q, engine=self.engine, parser=self.parser)
696696
tm.assert_frame_equal(result, expected)
@@ -854,7 +854,7 @@ def test_str_query_method(self, parser, engine):
854854
ops = 2 * ([eq] + [ne])
855855

856856
for lhs, op, rhs in zip(lhs, ops, rhs):
857-
ex = "{lhs} {op} {rhs}".format(lhs=lhs, op=op, rhs=rhs)
857+
ex = f"{lhs} {op} {rhs}"
858858
msg = r"'(Not)?In' nodes are not implemented"
859859
with pytest.raises(NotImplementedError, match=msg):
860860
df.query(
@@ -895,7 +895,7 @@ def test_str_list_query_method(self, parser, engine):
895895
ops = 2 * ([eq] + [ne])
896896

897897
for lhs, op, rhs in zip(lhs, ops, rhs):
898-
ex = "{lhs} {op} {rhs}".format(lhs=lhs, op=op, rhs=rhs)
898+
ex = f"{lhs} {op} {rhs}"
899899
with pytest.raises(NotImplementedError):
900900
df.query(ex, engine=engine, parser=parser)
901901
else:
@@ -1042,7 +1042,7 @@ def test_invalid_type_for_operator_raises(self, parser, engine, op):
10421042
msg = r"unsupported operand type\(s\) for .+: '.+' and '.+'"
10431043

10441044
with pytest.raises(TypeError, match=msg):
1045-
df.eval("a {0} b".format(op), engine=engine, parser=parser)
1045+
df.eval(f"a {op} b", engine=engine, parser=parser)
10461046

10471047

10481048
class TestDataFrameQueryBacktickQuoting:

0 commit comments

Comments
 (0)