-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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", | ||
|
@@ -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"]) | ||
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) | ||
|
@@ -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]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just define |
||
@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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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