4
4
import numpy as np
5
5
import pytest
6
6
7
+ from pandas .compat ._optional import import_optional_dependency
8
+
7
9
from pandas import option_context
8
10
import pandas ._testing as tm
9
11
from pandas .core .api import (
12
14
Series ,
13
15
)
14
16
from pandas .core .computation import expressions as expr
17
+ from pandas .util .version import Version
15
18
16
19
17
20
@pytest .fixture
@@ -324,7 +327,7 @@ def test_bool_ops_raise_on_arithmetic(self, op_str, opname):
324
327
@pytest .mark .parametrize (
325
328
"op_str,opname" , [("+" , "add" ), ("*" , "mul" ), ("-" , "sub" )]
326
329
)
327
- def test_bool_ops_warn_on_arithmetic (self , op_str , opname ):
330
+ def test_bool_ops_warn_on_arithmetic (self , op_str , opname , monkeypatch ):
328
331
n = 10
329
332
df = DataFrame (
330
333
{
@@ -343,36 +346,47 @@ def test_bool_ops_warn_on_arithmetic(self, op_str, opname):
343
346
# raises TypeError
344
347
return
345
348
346
- with tm .use_numexpr (True , min_elements = 5 ):
347
- with tm .assert_produces_warning ():
348
- r = f (df , df )
349
- e = fe (df , df )
350
- tm .assert_frame_equal (r , e )
351
-
352
- with tm .assert_produces_warning ():
353
- r = f (df .a , df .b )
354
- e = fe (df .a , df .b )
355
- tm .assert_series_equal (r , e )
356
-
357
- with tm .assert_produces_warning ():
358
- r = f (df .a , True )
359
- e = fe (df .a , True )
360
- tm .assert_series_equal (r , e )
361
-
362
- with tm .assert_produces_warning ():
363
- r = f (False , df .a )
364
- e = fe (False , df .a )
365
- tm .assert_series_equal (r , e )
366
-
367
- with tm .assert_produces_warning ():
368
- r = f (False , df )
369
- e = fe (False , df )
370
- tm .assert_frame_equal (r , e )
371
-
372
- with tm .assert_produces_warning ():
373
- r = f (df , True )
374
- e = fe (df , True )
375
- tm .assert_frame_equal (r , e )
349
+ msg = "operator is not supported by numexpr"
350
+ ne = import_optional_dependency ("numexpr" , errors = "ignore" )
351
+ warning = (
352
+ UserWarning
353
+ if ne
354
+ and op_str in {"+" , "*" }
355
+ and Version (ne .__version__ ) < Version ("2.13.1" )
356
+ else None
357
+ )
358
+ with monkeypatch .context () as m :
359
+ m .setattr (expr , "_MIN_ELEMENTS" , 5 )
360
+ with option_context ("compute.use_numexpr" , True ):
361
+ with tm .assert_produces_warning (warning , match = msg ):
362
+ r = f (df , df )
363
+ e = fe (df , df )
364
+ tm .assert_frame_equal (r , e )
365
+
366
+ with tm .assert_produces_warning (warning , match = msg ):
367
+ r = f (df .a , df .b )
368
+ e = fe (df .a , df .b )
369
+ tm .assert_series_equal (r , e )
370
+
371
+ with tm .assert_produces_warning (warning , match = msg ):
372
+ r = f (df .a , True )
373
+ e = fe (df .a , True )
374
+ tm .assert_series_equal (r , e )
375
+
376
+ with tm .assert_produces_warning (warning , match = msg ):
377
+ r = f (False , df .a )
378
+ e = fe (False , df .a )
379
+ tm .assert_series_equal (r , e )
380
+
381
+ with tm .assert_produces_warning (warning , match = msg ):
382
+ r = f (False , df )
383
+ e = fe (False , df )
384
+ tm .assert_frame_equal (r , e )
385
+
386
+ with tm .assert_produces_warning (warning , match = msg ):
387
+ r = f (df , True )
388
+ e = fe (df , True )
389
+ tm .assert_frame_equal (r , e )
376
390
377
391
@pytest .mark .parametrize (
378
392
"test_input,expected" ,
0 commit comments