Skip to content

BUG: compute.use_numexpr option not respected #42668

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 5 commits into from
Jul 28, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Numeric
^^^^^^^
- Bug in :meth:`DataFrame.rank` raising ``ValueError`` with ``object`` columns and ``method="first"`` (:issue:`41931`)
- Bug in :meth:`DataFrame.rank` treating missing values and extreme values as equal (for example ``np.nan`` and ``np.inf``), causing incorrect results when ``na_option="bottom"`` or ``na_option="top`` used (:issue:`41931`)
-
- Bug in ``numexpr`` engine still being used when the option ``compute.use_numexpr`` is set to ``False`` (:issue:`32556`)

Conversion
^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/computation/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ def _check_engine(engine: str | None) -> str:
Engine name.
"""
from pandas.core.computation.check import NUMEXPR_INSTALLED
from pandas.core.computation.expressions import USE_NUMEXPR

if engine is None:
engine = "numexpr" if NUMEXPR_INSTALLED else "python"
engine = "numexpr" if USE_NUMEXPR else "python"

if engine not in ENGINES:
valid_engines = list(ENGINES.keys())
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,32 @@ def test_invalid_engine():
pd.eval("x + y", local_dict={"x": 1, "y": 2}, engine="asdf")


@td.skip_if_no_ne
Copy link
Contributor

Choose a reason for hiding this comment

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

can you parameterize and text for both True/False here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@pytest.mark.parametrize("use_numexpr", [True, False])
def test_numexpr_option_respected(use_numexpr):
# GH 32556
from pandas.core.computation.eval import _check_engine

with pd.option_context("compute.use_numexpr", use_numexpr):
result = _check_engine(None)
if use_numexpr:
assert result == "numexpr"
else:
assert result == "python"
Copy link
Member

Choose a reason for hiding this comment

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

logic (e.g. if/else statements) in tests aren't ideal (https://testing.googleblog.com/2014/07/testing-on-toilet-dont-put-logic-in.html), how about something like

@pytest.mark.parametrize(
    ("use_numexpr", "expected"),
    (
          (True, "numexpr"),
          (False, "python"),
    )
)
def test_numexpr_option_respected(use_numexpr, expected):

and then, in the body, just assert result == expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is good to know! Thank you for linking the resource. Added!



@td.skip_if_no_ne
def test_numexpr_option_incompatible_op():
# GH 32556
with pd.option_context("compute.use_numexpr", False):
df = DataFrame(
{"A": [True, False, True, False, None, None], "B": [1, 2, 3, 4, 5, 6]}
)
result = df.query("A.isnull()")
expected = DataFrame({"A": [None, None], "B": [5, 6]}, index=[4, 5])
tm.assert_frame_equal(result, expected)


@td.skip_if_no_ne
def test_invalid_parser():
msg = "Invalid parser 'asdf' passed"
Expand Down