Skip to content

CLN: test_expressions.py #49422

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 2 commits into from
Nov 1, 2022
Merged
Changes from all commits
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
172 changes: 112 additions & 60 deletions pandas/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,88 @@
)
from pandas.core.computation import expressions as expr

_frame = DataFrame(np.random.randn(10001, 4), columns=list("ABCD"), dtype="float64")
_frame2 = DataFrame(np.random.randn(100, 4), columns=list("ABCD"), dtype="float64")
_mixed = DataFrame(
{
"A": _frame["A"].copy(),
"B": _frame["B"].astype("float32"),
"C": _frame["C"].astype("int64"),
"D": _frame["D"].astype("int32"),
}
)
_mixed2 = DataFrame(
{
"A": _frame2["A"].copy(),
"B": _frame2["B"].astype("float32"),
"C": _frame2["C"].astype("int64"),
"D": _frame2["D"].astype("int32"),
}
)
_integer = DataFrame(
np.random.randint(1, 100, size=(10001, 4)), columns=list("ABCD"), dtype="int64"
)
_integer2 = DataFrame(
np.random.randint(1, 100, size=(101, 4)), columns=list("ABCD"), dtype="int64"
)
_array = _frame["A"].values.copy()
_array2 = _frame2["A"].values.copy()

_array_mixed = _mixed["D"].values.copy()
_array_mixed2 = _mixed2["D"].values.copy()
@pytest.fixture
def _frame():
return DataFrame(np.random.randn(10001, 4), columns=list("ABCD"), dtype="float64")


@pytest.fixture
def _frame2():
return DataFrame(np.random.randn(100, 4), columns=list("ABCD"), dtype="float64")


@pytest.fixture
def _mixed(_frame):
return DataFrame(
{
"A": _frame["A"].copy(),
"B": _frame["B"].astype("float32"),
"C": _frame["C"].astype("int64"),
"D": _frame["D"].astype("int32"),
}
)


@pytest.fixture
def _mixed2(_frame2):
return DataFrame(
{
"A": _frame2["A"].copy(),
"B": _frame2["B"].astype("float32"),
"C": _frame2["C"].astype("int64"),
"D": _frame2["D"].astype("int32"),
}
)


@pytest.fixture
def _integer():
return DataFrame(
np.random.randint(1, 100, size=(10001, 4)), columns=list("ABCD"), dtype="int64"
)


@pytest.fixture
def _integer_randint(_integer):
# randint to get a case with zeros
return _integer * np.random.randint(0, 2, size=np.shape(_integer))


@pytest.fixture
def _integer2():
return DataFrame(
np.random.randint(1, 100, size=(101, 4)), columns=list("ABCD"), dtype="int64"
)


@pytest.fixture
def _array(_frame):
return _frame["A"].values.copy()


@pytest.fixture
def _array2(_frame2):
return _frame2["A"].values.copy()


@pytest.fixture
def _array_mixed(_mixed):
return _mixed["D"].values.copy()


@pytest.fixture
def _array_mixed2(_mixed2):
return _mixed2["D"].values.copy()


@pytest.mark.skipif(not expr.USE_NUMEXPR, reason="not using numexpr")
class TestExpressions:
def setup_method(self):
self._MIN_ELEMENTS = expr._MIN_ELEMENTS

def teardown_method(self):
expr._MIN_ELEMENTS = self._MIN_ELEMENTS
@pytest.fixture(autouse=True)
def save_min_elements(self):
min_elements = expr._MIN_ELEMENTS
yield
expr._MIN_ELEMENTS = min_elements

@staticmethod
def call_op(df, other, flex: bool, opname: str):
Expand All @@ -70,23 +114,23 @@ def call_op(df, other, flex: bool, opname: str):
return result, expected

@pytest.mark.parametrize(
"df",
"fixture",
[
_integer,
_integer2,
# randint to get a case with zeros
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
_frame,
_frame2,
_mixed,
_mixed2,
"_integer",
"_integer2",
"_integer_randint",
"_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):
def test_run_arithmetic(self, request, fixture, flex, arith):
df = request.getfixturevalue(fixture)
expr._MIN_ELEMENTS = 0
result, expected = self.call_op(df, df, flex, arith)

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

@pytest.mark.parametrize(
"df",
"fixture",
[
_integer,
_integer2,
# randint to get a case with zeros
_integer * np.random.randint(0, 2, size=np.shape(_integer)),
_frame,
_frame2,
_mixed,
_mixed2,
"_integer",
"_integer2",
"_integer_randint",
"_frame",
"_frame2",
"_mixed",
"_mixed2",
],
)
@pytest.mark.parametrize("flex", [True, False])
def test_run_binary(self, df, flex, comparison_op):
def test_run_binary(self, request, fixture, flex, comparison_op):
"""
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.
"""
df = request.getfixturevalue(fixture)
arith = comparison_op.__name__
with option_context("compute.use_numexpr", False):
other = df.copy() + 1
Expand Down Expand Up @@ -160,9 +204,12 @@ def test_invalid(self):
[("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"), ("pow", "**")],
)
@pytest.mark.parametrize(
"left,right", [(_array, _array2), (_array_mixed, _array_mixed2)]
"left_fix,right_fix", [("_array", "_array2"), ("_array_mixed", "_array_mixed2")]
)
def test_binary_ops(self, opname, op_str, left, right):
def test_binary_ops(self, request, opname, op_str, left_fix, right_fix):
left = request.getfixturevalue(left_fix)
right = request.getfixturevalue(right_fix)

def testit():

if opname == "pow":
Expand Down Expand Up @@ -202,9 +249,12 @@ def testit():
],
)
@pytest.mark.parametrize(
"left,right", [(_array, _array2), (_array_mixed, _array_mixed2)]
"left_fix,right_fix", [("_array", "_array2"), ("_array_mixed", "_array_mixed2")]
)
def test_comparison_ops(self, opname, op_str, left, right):
def test_comparison_ops(self, request, opname, op_str, left_fix, right_fix):
left = request.getfixturevalue(left_fix)
right = request.getfixturevalue(right_fix)

def testit():
f12 = left + 1
f22 = right + 1
Expand All @@ -227,8 +277,10 @@ def testit():
testit()

@pytest.mark.parametrize("cond", [True, False])
@pytest.mark.parametrize("df", [_frame, _frame2, _mixed, _mixed2])
def test_where(self, cond, df):
@pytest.mark.parametrize("fixture", ["_frame", "_frame2", "_mixed", "_mixed2"])
def test_where(self, request, cond, fixture):
df = request.getfixturevalue(fixture)

def testit():
c = np.empty(df.shape, dtype=np.bool_)
c.fill(cond)
Expand Down Expand Up @@ -350,7 +402,7 @@ def test_bool_ops_column_name_dtype(self, test_input, expected):
"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, axis, arith, _frame):
# GH#26736 Dataframe.floordiv(Series, axis=1) fails

df = _frame
Expand Down