Skip to content

TST: Refactor test_expressions.py #44778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 55 additions & 51 deletions pandas/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@
@pytest.mark.skipif(not expr.USE_NUMEXPR, reason="not using numexpr")
class TestExpressions:
def setup_method(self, method):

self.frame = _frame.copy()
self.frame2 = _frame2.copy()
self.mixed = _mixed.copy()
self.mixed2 = _mixed2.copy()
self._MIN_ELEMENTS = expr._MIN_ELEMENTS

def teardown_method(self, method):
Expand All @@ -75,50 +70,36 @@ def call_op(df, other, flex: bool, opname: str):
result = op(df, other)
return result, expected

def run_arithmetic(self, df, other, flex: bool):
expr._MIN_ELEMENTS = 0
operations = ["add", "sub", "mul", "mod", "truediv", "floordiv"]
for arith in operations:
result, expected = self.call_op(df, other, flex, arith)

if arith == "truediv":
if expected.ndim == 1:
assert expected.dtype.kind == "f"
else:
assert all(x.kind == "f" for x in expected.dtypes.values)
tm.assert_equal(expected, result)

def run_binary(self, df, other, flex: bool):
"""
tests solely that the result is the same whether or not numexpr is
enabled. Need to test whether the function does the correct thing
elsewhere.
"""
@pytest.mark.parametrize(
"df",
[
_integer,
_integer2,
# randint to get a case with zeros
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
_frame,
_frame2,
_mixed,
_mixed2,
],
)
@pytest.mark.parametrize("flex", [True, False])
@pytest.mark.parametrize(
"arith", ["add", "sub", "mul", "mod", "truediv", "floordiv"]
)
def test_run_arithmetic(self, df, flex, arith):
expr._MIN_ELEMENTS = 0
expr.set_test_mode(True)
operations = ["gt", "lt", "ge", "le", "eq", "ne"]

for arith in operations:
result, expected = self.call_op(df, other, flex, arith)

used_numexpr = expr.get_test_result()
assert used_numexpr, "Did not use numexpr as expected."
tm.assert_equal(expected, result)
result, expected = self.call_op(df, df, flex, arith)

def run_frame(self, df, other, flex: bool):
self.run_arithmetic(df, other, flex)

set_option("compute.use_numexpr", False)
binary_comp = other + 1
set_option("compute.use_numexpr", True)
self.run_binary(df, binary_comp, flex)
if arith == "truediv":
assert all(x.kind == "f" for x in expected.dtypes.values)
tm.assert_equal(expected, result)

for i in range(len(df.columns)):
self.run_arithmetic(df.iloc[:, i], other.iloc[:, i], flex)
# FIXME: dont leave commented-out
# series doesn't uses vec_compare instead of numexpr...
# binary_comp = other.iloc[:, i] + 1
# self.run_binary(df.iloc[:, i], binary_comp, flex)
result, expected = self.call_op(df.iloc[:, i], df.iloc[:, i], flex, arith)
if arith == "truediv":
assert expected.dtype.kind == "f"
tm.assert_equal(expected, result)

@pytest.mark.parametrize(
"df",
Expand All @@ -134,8 +115,31 @@ def run_frame(self, df, other, flex: bool):
],
)
@pytest.mark.parametrize("flex", [True, False])
def test_arithmetic(self, df, flex):
self.run_frame(df, df, flex)
@pytest.mark.parametrize("arith", ["gt", "lt", "ge", "le", "eq", "ne"])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have a fixture for this

def test_run_binary(self, df, flex, arith):
"""
tests solely that the result is the same whether or not numexpr is
enabled. Need to test whether the function does the correct thing
elsewhere.
"""
set_option("compute.use_numexpr", False)
other = df.copy() + 1
set_option("compute.use_numexpr", True)

expr._MIN_ELEMENTS = 0
expr.set_test_mode(True)

result, expected = self.call_op(df, other, flex, arith)

used_numexpr = expr.get_test_result()
assert used_numexpr, "Did not use numexpr as expected."
tm.assert_equal(expected, result)

# FIXME: dont leave commented-out
# series doesn't uses vec_compare instead of numexpr...
# for i in range(len(df.columns)):
# binary_comp = other.iloc[:, i] + 1
# self.run_binary(df.iloc[:, i], binary_comp, flex)

def test_invalid(self):
array = np.random.randn(1_000_001)
Expand Down Expand Up @@ -344,18 +348,18 @@ def test_bool_ops_column_name_dtype(self, test_input, expected):
result = test_input.loc[:, ["a", "dtype"]].ne(test_input.loc[:, ["a", "dtype"]])
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("df", [_frame])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just define df = _frame' inside?

@pytest.mark.parametrize(
"arith", ("add", "sub", "mul", "mod", "truediv", "floordiv")
)
@pytest.mark.parametrize("axis", (0, 1))
def test_frame_series_axis(self, axis, arith):
def test_frame_series_axis(self, df, axis, arith):
# GH#26736 Dataframe.floordiv(Series, axis=1) fails

df = self.frame
if axis == 1:
other = self.frame.iloc[0, :]
other = df.iloc[0, :]
else:
other = self.frame.iloc[:, 0]
other = df.iloc[:, 0]

expr._MIN_ELEMENTS = 0

Expand Down