@@ -78,45 +78,48 @@ def test_query_numexpr(self):
78
78
79
79
80
80
class TestDataFrameEval :
81
- def test_ops (self ):
81
+
82
+ # smaller hits python, larger hits numexpr
83
+ @pytest .mark .parametrize ("n" , [4 , 4000 ])
84
+ @pytest .mark .parametrize (
85
+ "op_str,op,rop" ,
86
+ [
87
+ ("+" , "__add__" , "__radd__" ),
88
+ ("-" , "__sub__" , "__rsub__" ),
89
+ ("*" , "__mul__" , "__rmul__" ),
90
+ ("/" , "__truediv__" , "__rtruediv__" ),
91
+ ],
92
+ )
93
+ def test_ops (self , op_str , op , rop , n ):
82
94
83
95
# tst ops and reversed ops in evaluation
84
96
# GH7198
85
97
86
- # smaller hits python, larger hits numexpr
87
- for n in [4 , 4000 ]:
88
-
89
- df = DataFrame (1 , index = range (n ), columns = list ("abcd" ))
90
- df .iloc [0 ] = 2
91
- m = df .mean ()
98
+ df = DataFrame (1 , index = range (n ), columns = list ("abcd" ))
99
+ df .iloc [0 ] = 2
100
+ m = df .mean ()
92
101
93
- for op_str , op , rop in [
94
- ("+" , "__add__" , "__radd__" ),
95
- ("-" , "__sub__" , "__rsub__" ),
96
- ("*" , "__mul__" , "__rmul__" ),
97
- ("/" , "__truediv__" , "__rtruediv__" ),
98
- ]:
99
-
100
- base = DataFrame ( # noqa
101
- np .tile (m .values , n ).reshape (n , - 1 ), columns = list ("abcd" )
102
- )
102
+ base = DataFrame ( # noqa
103
+ np .tile (m .values , n ).reshape (n , - 1 ), columns = list ("abcd" )
104
+ )
103
105
104
- expected = eval ("base{op} df" . format ( op = op_str ) )
106
+ expected = eval (f "base { op_str } df" )
105
107
106
- # ops as strings
107
- result = eval ("m{op} df". format ( op = op_str ) )
108
- tm .assert_frame_equal (result , expected )
108
+ # ops as strings
109
+ result = eval (f"m { op_str } df" )
110
+ tm .assert_frame_equal (result , expected )
109
111
110
- # these are commutative
111
- if op in ["+" , "*" ]:
112
- result = getattr (df , op )(m )
113
- tm .assert_frame_equal (result , expected )
112
+ # these are commutative
113
+ if op in ["+" , "*" ]:
114
+ result = getattr (df , op )(m )
115
+ tm .assert_frame_equal (result , expected )
114
116
115
- # these are not
116
- elif op in ["-" , "/" ]:
117
- result = getattr (df , rop )(m )
118
- tm .assert_frame_equal (result , expected )
117
+ # these are not
118
+ elif op in ["-" , "/" ]:
119
+ result = getattr (df , rop )(m )
120
+ tm .assert_frame_equal (result , expected )
119
121
122
+ def test_dataframe_sub_numexpr_path (self ):
120
123
# GH7192: Note we need a large number of rows to ensure this
121
124
# goes through the numexpr path
122
125
df = DataFrame (dict (A = np .random .randn (25000 )))
@@ -451,9 +454,7 @@ def test_date_query_with_non_date(self):
451
454
452
455
for op in ["<" , ">" , "<=" , ">=" ]:
453
456
with pytest .raises (TypeError ):
454
- df .query (
455
- "dates {op} nondate" .format (op = op ), parser = parser , engine = engine
456
- )
457
+ df .query (f"dates { op } nondate" , parser = parser , engine = engine )
457
458
458
459
def test_query_syntax_error (self ):
459
460
engine , parser = self .engine , self .parser
@@ -687,10 +688,9 @@ def test_inf(self):
687
688
n = 10
688
689
df = DataFrame ({"a" : np .random .rand (n ), "b" : np .random .rand (n )})
689
690
df .loc [::2 , 0 ] = np .inf
690
- ops = "==" , "!="
691
- d = dict (zip (ops , (operator .eq , operator .ne )))
691
+ d = {"==" : operator .eq , "!=" : operator .ne }
692
692
for op , f in d .items ():
693
- q = "a {op} inf" . format ( op = op )
693
+ q = f "a { op } inf"
694
694
expected = df [f (df .a , np .inf )]
695
695
result = df .query (q , engine = self .engine , parser = self .parser )
696
696
tm .assert_frame_equal (result , expected )
@@ -854,7 +854,7 @@ def test_str_query_method(self, parser, engine):
854
854
ops = 2 * ([eq ] + [ne ])
855
855
856
856
for lhs , op , rhs in zip (lhs , ops , rhs ):
857
- ex = "{lhs} {op} {rhs}" . format ( lhs = lhs , op = op , rhs = rhs )
857
+ ex = f "{ lhs } { op } { rhs } "
858
858
msg = r"'(Not)?In' nodes are not implemented"
859
859
with pytest .raises (NotImplementedError , match = msg ):
860
860
df .query (
@@ -895,7 +895,7 @@ def test_str_list_query_method(self, parser, engine):
895
895
ops = 2 * ([eq ] + [ne ])
896
896
897
897
for lhs , op , rhs in zip (lhs , ops , rhs ):
898
- ex = "{lhs} {op} {rhs}" . format ( lhs = lhs , op = op , rhs = rhs )
898
+ ex = f "{ lhs } { op } { rhs } "
899
899
with pytest .raises (NotImplementedError ):
900
900
df .query (ex , engine = engine , parser = parser )
901
901
else :
@@ -1042,7 +1042,7 @@ def test_invalid_type_for_operator_raises(self, parser, engine, op):
1042
1042
msg = r"unsupported operand type\(s\) for .+: '.+' and '.+'"
1043
1043
1044
1044
with pytest .raises (TypeError , match = msg ):
1045
- df .eval ("a {0 } b" . format ( op ) , engine = engine , parser = parser )
1045
+ df .eval (f "a { op } b" , engine = engine , parser = parser )
1046
1046
1047
1047
1048
1048
class TestDataFrameQueryBacktickQuoting :
0 commit comments