Skip to content

Commit 5ee8cf1

Browse files
TST: make tests involving use_numexpr more robust (#44601)
1 parent 2f203d1 commit 5ee8cf1

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

pandas/_testing/contexts.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
import numpy as np
1616

17+
from pandas import set_option
18+
1719
from pandas.io.common import get_handle
1820

1921

@@ -202,11 +204,11 @@ def use_numexpr(use, min_elements=None):
202204

203205
olduse = expr.USE_NUMEXPR
204206
oldmin = expr._MIN_ELEMENTS
205-
expr.set_use_numexpr(use)
207+
set_option("compute.use_numexpr", use)
206208
expr._MIN_ELEMENTS = min_elements
207209
yield
208210
expr._MIN_ELEMENTS = oldmin
209-
expr.set_use_numexpr(olduse)
211+
set_option("compute.use_numexpr", olduse)
210212

211213

212214
class RNGContext:

pandas/tests/test_downstream.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import pandas.util._test_decorators as td
1212

13+
import pandas as pd
1314
from pandas import DataFrame
1415
import pandas._testing as tm
1516

@@ -32,14 +33,21 @@ def df():
3233
@pytest.mark.filterwarnings("ignore:.*64Index is deprecated:FutureWarning")
3334
def test_dask(df):
3435

35-
toolz = import_module("toolz") # noqa:F841
36-
dask = import_module("dask") # noqa:F841
36+
# dask sets "compute.use_numexpr" to False, so catch the current value
37+
# and ensure to reset it afterwards to avoid impacting other tests
38+
olduse = pd.get_option("compute.use_numexpr")
3739

38-
import dask.dataframe as dd
40+
try:
41+
toolz = import_module("toolz") # noqa:F841
42+
dask = import_module("dask") # noqa:F841
43+
44+
import dask.dataframe as dd
3945

40-
ddf = dd.from_pandas(df, npartitions=3)
41-
assert ddf.A is not None
42-
assert ddf.compute() is not None
46+
ddf = dd.from_pandas(df, npartitions=3)
47+
assert ddf.A is not None
48+
assert ddf.compute() is not None
49+
finally:
50+
pd.set_option("compute.use_numexpr", olduse)
4351

4452

4553
def test_xarray(df):

pandas/tests/test_expressions.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
import pytest
77

8+
from pandas import set_option
89
import pandas._testing as tm
910
from pandas.core.api import (
1011
DataFrame,
@@ -65,9 +66,9 @@ def call_op(df, other, flex: bool, opname: str):
6566
else:
6667
op = getattr(operator, opname)
6768

68-
expr.set_use_numexpr(False)
69+
set_option("compute.use_numexpr", False)
6970
expected = op(df, other)
70-
expr.set_use_numexpr(True)
71+
set_option("compute.use_numexpr", True)
7172

7273
expr.get_test_result()
7374

@@ -107,9 +108,9 @@ def run_binary(self, df, other, flex: bool):
107108
def run_frame(self, df, other, flex: bool):
108109
self.run_arithmetic(df, other, flex)
109110

110-
expr.set_use_numexpr(False)
111+
set_option("compute.use_numexpr", False)
111112
binary_comp = other + 1
112-
expr.set_use_numexpr(True)
113+
set_option("compute.use_numexpr", True)
113114
self.run_binary(df, binary_comp, flex)
114115

115116
for i in range(len(df.columns)):
@@ -179,9 +180,9 @@ def testit():
179180
result = expr._can_use_numexpr(op, op_str, right, right, "evaluate")
180181
assert not result
181182

182-
expr.set_use_numexpr(False)
183+
set_option("compute.use_numexpr", False)
183184
testit()
184-
expr.set_use_numexpr(True)
185+
set_option("compute.use_numexpr", True)
185186
expr.set_numexpr_threads(1)
186187
testit()
187188
expr.set_numexpr_threads()
@@ -215,9 +216,9 @@ def testit():
215216
result = expr._can_use_numexpr(op, op_str, right, f22, "evaluate")
216217
assert not result
217218

218-
expr.set_use_numexpr(False)
219+
set_option("compute.use_numexpr", False)
219220
testit()
220-
expr.set_use_numexpr(True)
221+
set_option("compute.use_numexpr", True)
221222
expr.set_numexpr_threads(1)
222223
testit()
223224
expr.set_numexpr_threads()
@@ -233,9 +234,9 @@ def testit():
233234
expected = np.where(c, df.values, df.values + 1)
234235
tm.assert_numpy_array_equal(result, expected)
235236

236-
expr.set_use_numexpr(False)
237+
set_option("compute.use_numexpr", False)
237238
testit()
238-
expr.set_use_numexpr(True)
239+
set_option("compute.use_numexpr", True)
239240
expr.set_numexpr_threads(1)
240241
testit()
241242
expr.set_numexpr_threads()
@@ -360,9 +361,9 @@ def test_frame_series_axis(self, axis, arith):
360361

361362
op_func = getattr(df, arith)
362363

363-
expr.set_use_numexpr(False)
364+
set_option("compute.use_numexpr", False)
364365
expected = op_func(other, axis=axis)
365-
expr.set_use_numexpr(True)
366+
set_option("compute.use_numexpr", True)
366367

367368
result = op_func(other, axis=axis)
368369
tm.assert_frame_equal(expected, result)
@@ -387,9 +388,9 @@ def test_python_semantics_with_numexpr_installed(self, op, box, scalar):
387388
result = method(scalar)
388389

389390
# compare result with numpy
390-
expr.set_use_numexpr(False)
391+
set_option("compute.use_numexpr", False)
391392
expected = method(scalar)
392-
expr.set_use_numexpr(True)
393+
set_option("compute.use_numexpr", True)
393394
tm.assert_equal(result, expected)
394395

395396
# compare result element-wise with Python

0 commit comments

Comments
 (0)