44import numpy as np
55import pytest
66
7+ from pandas .compat ._optional import import_optional_dependency
8+
79from pandas import option_context
810import pandas ._testing as tm
911from pandas .core .api import (
1214 Series ,
1315)
1416from pandas .core .computation import expressions as expr
17+ from pandas .util .version import Version
1518
1619
1720@pytest .fixture
@@ -324,7 +327,7 @@ def test_bool_ops_raise_on_arithmetic(self, op_str, opname):
324327 @pytest .mark .parametrize (
325328 "op_str,opname" , [("+" , "add" ), ("*" , "mul" ), ("-" , "sub" )]
326329 )
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 ):
328331 n = 10
329332 df = DataFrame (
330333 {
@@ -343,36 +346,47 @@ def test_bool_ops_warn_on_arithmetic(self, op_str, opname):
343346 # raises TypeError
344347 return
345348
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 )
376390
377391 @pytest .mark .parametrize (
378392 "test_input,expected" ,
0 commit comments