42
42
engine ,
43
43
marks = pytest .mark .skipif (
44
44
engine == "numexpr" and not _USE_NUMEXPR ,
45
- reason = "numexpr enabled->{enabled}, "
46
- "installed->{installed}" .format (
47
- enabled = _USE_NUMEXPR , installed = _NUMEXPR_INSTALLED
48
- ),
45
+ reason = f"numexpr enabled->{ _USE_NUMEXPR } , "
46
+ f"installed->{ _NUMEXPR_INSTALLED } " ,
49
47
),
50
48
)
51
49
for engine in _engines
@@ -189,9 +187,7 @@ def test_complex_cmp_ops(self, cmp1, cmp2):
189
187
rhs_new = _eval_single_bin (lhs , cmp2 , rhs , self .engine )
190
188
expected = _eval_single_bin (lhs_new , binop , rhs_new , self .engine )
191
189
192
- ex = "(lhs {cmp1} rhs) {binop} (lhs {cmp2} rhs)" .format (
193
- cmp1 = cmp1 , binop = binop , cmp2 = cmp2
194
- )
190
+ ex = f"(lhs { cmp1 } rhs) { binop } (lhs { cmp2 } rhs)"
195
191
result = pd .eval (ex , engine = self .engine , parser = self .parser )
196
192
self .check_equal (result , expected )
197
193
@@ -265,9 +261,9 @@ def check_operands(left, right, cmp_op):
265
261
rhs_new = check_operands (mid , rhs , cmp2 )
266
262
267
263
if lhs_new is not None and rhs_new is not None :
268
- ex1 = "lhs {0 } mid {1 } rhs" . format ( cmp1 , cmp2 )
269
- ex2 = "lhs {0 } mid and mid {1 } rhs" . format ( cmp1 , cmp2 )
270
- ex3 = "(lhs {0 } mid) & (mid {1 } rhs)" . format ( cmp1 , cmp2 )
264
+ ex1 = f "lhs { cmp1 } mid { cmp2 } rhs"
265
+ ex2 = f "lhs { cmp1 } mid and mid { cmp2 } rhs"
266
+ ex3 = f "(lhs { cmp1 } mid) & (mid { cmp2 } rhs)"
271
267
expected = _eval_single_bin (lhs_new , "&" , rhs_new , self .engine )
272
268
273
269
for ex in (ex1 , ex2 , ex3 ):
@@ -276,7 +272,7 @@ def check_operands(left, right, cmp_op):
276
272
tm .assert_almost_equal (result , expected )
277
273
278
274
def check_simple_cmp_op (self , lhs , cmp1 , rhs ):
279
- ex = "lhs {0 } rhs" . format ( cmp1 )
275
+ ex = f "lhs { cmp1 } rhs"
280
276
msg = (
281
277
r"only list-like( or dict-like)? objects are allowed to be"
282
278
r" passed to (DataFrame\.)?isin\(\), you passed a"
@@ -297,12 +293,12 @@ def check_simple_cmp_op(self, lhs, cmp1, rhs):
297
293
self .check_equal (result , expected )
298
294
299
295
def check_binary_arith_op (self , lhs , arith1 , rhs ):
300
- ex = "lhs {0 } rhs" . format ( arith1 )
296
+ ex = f "lhs { arith1 } rhs"
301
297
result = pd .eval (ex , engine = self .engine , parser = self .parser )
302
298
expected = _eval_single_bin (lhs , arith1 , rhs , self .engine )
303
299
304
300
tm .assert_almost_equal (result , expected )
305
- ex = "lhs {0 } rhs {0 } rhs" . format ( arith1 )
301
+ ex = f "lhs { arith1 } rhs { arith1 } rhs"
306
302
result = pd .eval (ex , engine = self .engine , parser = self .parser )
307
303
nlhs = _eval_single_bin (lhs , arith1 , rhs , self .engine )
308
304
self .check_alignment (result , nlhs , rhs , arith1 )
@@ -317,25 +313,25 @@ def check_alignment(self, result, nlhs, ghs, op):
317
313
else :
318
314
319
315
# direct numpy comparison
320
- expected = self .ne .evaluate ("nlhs {0 } ghs" . format ( op ) )
316
+ expected = self .ne .evaluate (f "nlhs { op } ghs" )
321
317
tm .assert_numpy_array_equal (result .values , expected )
322
318
323
319
# modulus, pow, and floor division require special casing
324
320
325
321
def check_modulus (self , lhs , arith1 , rhs ):
326
- ex = "lhs {0 } rhs" . format ( arith1 )
322
+ ex = f "lhs { arith1 } rhs"
327
323
result = pd .eval (ex , engine = self .engine , parser = self .parser )
328
324
expected = lhs % rhs
329
325
330
326
tm .assert_almost_equal (result , expected )
331
- expected = self .ne .evaluate ("expected {0 } rhs" . format ( arith1 ) )
327
+ expected = self .ne .evaluate (f "expected { arith1 } rhs" )
332
328
if isinstance (result , (DataFrame , Series )):
333
329
tm .assert_almost_equal (result .values , expected )
334
330
else :
335
331
tm .assert_almost_equal (result , expected .item ())
336
332
337
333
def check_floor_division (self , lhs , arith1 , rhs ):
338
- ex = "lhs {0 } rhs" . format ( arith1 )
334
+ ex = f "lhs { arith1 } rhs"
339
335
340
336
if self .engine == "python" :
341
337
res = pd .eval (ex , engine = self .engine , parser = self .parser )
@@ -370,7 +366,7 @@ def get_expected_pow_result(self, lhs, rhs):
370
366
return expected
371
367
372
368
def check_pow (self , lhs , arith1 , rhs ):
373
- ex = "lhs {0 } rhs" . format ( arith1 )
369
+ ex = f "lhs { arith1 } rhs"
374
370
expected = self .get_expected_pow_result (lhs , rhs )
375
371
result = pd .eval (ex , engine = self .engine , parser = self .parser )
376
372
@@ -384,7 +380,7 @@ def check_pow(self, lhs, arith1, rhs):
384
380
else :
385
381
tm .assert_almost_equal (result , expected )
386
382
387
- ex = "(lhs {0 } rhs) {0 } rhs" . format ( arith1 )
383
+ ex = f "(lhs { arith1 } rhs) { arith1 } rhs"
388
384
result = pd .eval (ex , engine = self .engine , parser = self .parser )
389
385
expected = self .get_expected_pow_result (
390
386
self .get_expected_pow_result (lhs , rhs ), rhs
@@ -409,7 +405,7 @@ def check_single_invert_op(self, lhs, cmp1, rhs):
409
405
410
406
def check_compound_invert_op (self , lhs , cmp1 , rhs ):
411
407
skip_these = ["in" , "not in" ]
412
- ex = "~(lhs {0 } rhs)" . format ( cmp1 )
408
+ ex = f "~(lhs { cmp1 } rhs)"
413
409
414
410
msg = (
415
411
r"only list-like( or dict-like)? objects are allowed to be"
@@ -443,7 +439,7 @@ def check_compound_invert_op(self, lhs, cmp1, rhs):
443
439
tm .assert_almost_equal (ev , result )
444
440
445
441
def ex (self , op , var_name = "lhs" ):
446
- return "{0}{1}" . format ( op , var_name )
442
+ return f" { op } { var_name } "
447
443
448
444
def test_frame_invert (self ):
449
445
expr = self .ex ("~" )
@@ -733,16 +729,16 @@ def test_float_truncation(self):
733
729
734
730
df = pd .DataFrame ({"A" : [1000000000.0009 , 1000000000.0011 , 1000000000.0015 ]})
735
731
cutoff = 1000000000.0006
736
- result = df .query ("A < {cutoff:.4f}" . format ( cutoff = cutoff ) )
732
+ result = df .query (f "A < { cutoff :.4f} " )
737
733
assert result .empty
738
734
739
735
cutoff = 1000000000.0010
740
- result = df .query ("A > {cutoff:.4f}" . format ( cutoff = cutoff ) )
736
+ result = df .query (f "A > { cutoff :.4f} " )
741
737
expected = df .loc [[1 , 2 ], :]
742
738
tm .assert_frame_equal (expected , result )
743
739
744
740
exact = 1000000000.0011
745
- result = df .query ("A == {exact:.4f}" . format ( exact = exact ) )
741
+ result = df .query (f "A == { exact :.4f} " )
746
742
expected = df .loc [[1 ], :]
747
743
tm .assert_frame_equal (expected , result )
748
744
@@ -781,7 +777,7 @@ def setup_ops(self):
781
777
self .unary_ops = "+" , "-" , "~"
782
778
783
779
def check_chained_cmp_op (self , lhs , cmp1 , mid , cmp2 , rhs ):
784
- ex1 = "lhs {0 } mid {1 } rhs" . format ( cmp1 , cmp2 )
780
+ ex1 = f "lhs { cmp1 } mid { cmp2 } rhs"
785
781
with pytest .raises (NotImplementedError ):
786
782
pd .eval (ex1 , engine = self .engine , parser = self .parser )
787
783
@@ -794,7 +790,7 @@ def setup_class(cls):
794
790
cls .parser = "python"
795
791
796
792
def check_modulus (self , lhs , arith1 , rhs ):
797
- ex = "lhs {0 } rhs" . format ( arith1 )
793
+ ex = f "lhs { arith1 } rhs"
798
794
result = pd .eval (ex , engine = self .engine , parser = self .parser )
799
795
800
796
expected = lhs % rhs
@@ -811,7 +807,7 @@ def check_alignment(self, result, nlhs, ghs, op):
811
807
# TypeError, AttributeError: series or frame with scalar align
812
808
pass
813
809
else :
814
- expected = eval ("nlhs {0 } ghs" . format ( op ) )
810
+ expected = eval (f "nlhs { op } ghs" )
815
811
tm .assert_almost_equal (result , expected )
816
812
817
813
@@ -840,13 +836,13 @@ class TestTypeCasting:
840
836
@pytest .mark .parametrize ("dt" , [np .float32 , np .float64 ])
841
837
def test_binop_typecasting (self , engine , parser , op , dt ):
842
838
df = tm .makeCustomDataframe (5 , 3 , data_gen_f = f , dtype = dt )
843
- s = "df {} 3" . format ( op )
839
+ s = f "df { op } 3"
844
840
res = pd .eval (s , engine = engine , parser = parser )
845
841
assert df .values .dtype == dt
846
842
assert res .values .dtype == dt
847
843
tm .assert_frame_equal (res , eval (s ))
848
844
849
- s = "3 {} df" . format ( op )
845
+ s = f "3 { op } df"
850
846
res = pd .eval (s , engine = engine , parser = parser )
851
847
assert df .values .dtype == dt
852
848
assert res .values .dtype == dt
@@ -1013,8 +1009,8 @@ def test_series_frame_commutativity(self, engine, parser):
1013
1009
index = getattr (df , index_name )
1014
1010
s = Series (np .random .randn (5 ), index [:5 ])
1015
1011
1016
- lhs = "s {0 } df" . format ( op )
1017
- rhs = "df {0 } s" . format ( op )
1012
+ lhs = f "s { op } df"
1013
+ rhs = f "df { op } s"
1018
1014
if should_warn (df .index , s .index ):
1019
1015
with tm .assert_produces_warning (RuntimeWarning ):
1020
1016
a = pd .eval (lhs , engine = engine , parser = parser )
@@ -1149,9 +1145,9 @@ def test_simple_arith_ops(self):
1149
1145
ops = self .arith_ops
1150
1146
1151
1147
for op in filter (lambda x : x != "//" , ops ):
1152
- ex = "1 {0 } 1" . format ( op )
1153
- ex2 = "x {0 } 1" . format ( op )
1154
- ex3 = "1 {0 } (x + 1)" . format ( op )
1148
+ ex = f "1 { op } 1"
1149
+ ex2 = f "x { op } 1"
1150
+ ex3 = f "1 { op } (x + 1)"
1155
1151
1156
1152
if op in ("in" , "not in" ):
1157
1153
msg = "argument of type 'int' is not iterable"
@@ -1176,7 +1172,7 @@ def test_simple_arith_ops(self):
1176
1172
1177
1173
def test_simple_bool_ops (self ):
1178
1174
for op , lhs , rhs in product (expr ._bool_ops_syms , (True , False ), (True , False )):
1179
- ex = "{0 } {1 } {2}" . format ( lhs , op , rhs )
1175
+ ex = f" { lhs } { op } { rhs } "
1180
1176
res = self .eval (ex )
1181
1177
exp = eval (ex )
1182
1178
assert res == exp
@@ -1185,7 +1181,7 @@ def test_bool_ops_with_constants(self):
1185
1181
for op , lhs , rhs in product (
1186
1182
expr ._bool_ops_syms , ("True" , "False" ), ("True" , "False" )
1187
1183
):
1188
- ex = "{0 } {1 } {2}" . format ( lhs , op , rhs )
1184
+ ex = f" { lhs } { op } { rhs } "
1189
1185
res = self .eval (ex )
1190
1186
exp = eval (ex )
1191
1187
assert res == exp
@@ -1679,7 +1675,7 @@ def test_bool_ops_with_constants(self):
1679
1675
for op , lhs , rhs in product (
1680
1676
expr ._bool_ops_syms , ("True" , "False" ), ("True" , "False" )
1681
1677
):
1682
- ex = "{0 } {1 } {2}" . format ( lhs , op , rhs )
1678
+ ex = f" { lhs } { op } { rhs } "
1683
1679
if op in ("and" , "or" ):
1684
1680
with pytest .raises (NotImplementedError ):
1685
1681
self .eval (ex )
@@ -1690,7 +1686,7 @@ def test_bool_ops_with_constants(self):
1690
1686
1691
1687
def test_simple_bool_ops (self ):
1692
1688
for op , lhs , rhs in product (expr ._bool_ops_syms , (True , False ), (True , False )):
1693
- ex = "lhs {0 } rhs" . format ( op )
1689
+ ex = f "lhs { op } rhs"
1694
1690
if op in ("and" , "or" ):
1695
1691
with pytest .raises (NotImplementedError ):
1696
1692
pd .eval (ex , engine = self .engine , parser = self .parser )
@@ -1742,25 +1738,25 @@ def test_unary_functions(self, unary_fns_for_ne):
1742
1738
a = df .a
1743
1739
1744
1740
for fn in unary_fns_for_ne :
1745
- expr = "{0 }(a)". format ( fn )
1741
+ expr = f" { fn } (a)"
1746
1742
got = self .eval (expr )
1747
1743
with np .errstate (all = "ignore" ):
1748
1744
expect = getattr (np , fn )(a )
1749
1745
tm .assert_series_equal (got , expect , check_names = False )
1750
1746
1751
1747
def test_floor_and_ceil_functions_raise_error (self , ne_lt_2_6_9 , unary_fns_for_ne ):
1752
1748
for fn in ("floor" , "ceil" ):
1753
- msg = '"{0 }" is not a supported function' . format ( fn )
1749
+ msg = f '"{ fn } " is not a supported function'
1754
1750
with pytest .raises (ValueError , match = msg ):
1755
- expr = "{0 }(100)". format ( fn )
1751
+ expr = f" { fn } (100)"
1756
1752
self .eval (expr )
1757
1753
1758
1754
def test_binary_functions (self ):
1759
1755
df = DataFrame ({"a" : np .random .randn (10 ), "b" : np .random .randn (10 )})
1760
1756
a = df .a
1761
1757
b = df .b
1762
1758
for fn in self .binary_fns :
1763
- expr = "{0 }(a, b)". format ( fn )
1759
+ expr = f" { fn } (a, b)"
1764
1760
got = self .eval (expr )
1765
1761
with np .errstate (all = "ignore" ):
1766
1762
expect = getattr (np , fn )(a , b )
@@ -1971,9 +1967,9 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
1971
1967
lhs = gen [lhs ]() # noqa
1972
1968
rhs = gen [rhs ]() # noqa
1973
1969
1974
- ex1 = "lhs {0 } mid {1 } rhs" . format ( cmp , cmp )
1975
- ex2 = "lhs {0 } mid and mid {1 } rhs" . format ( cmp , cmp )
1976
- ex3 = "(lhs {0 } mid) & (mid {1 } rhs)" . format ( cmp , cmp )
1970
+ ex1 = f "lhs { cmp } mid { cmp } rhs"
1971
+ ex2 = f "lhs { cmp } mid and mid { cmp } rhs"
1972
+ ex3 = f "(lhs { cmp } mid) & (mid { cmp } rhs)"
1977
1973
for ex in (ex1 , ex2 , ex3 ):
1978
1974
with pytest .raises (NotImplementedError ):
1979
1975
pd .eval (ex , engine = engine , parser = parser )
@@ -1990,7 +1986,7 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
1990
1986
)
1991
1987
def test_equals_various (other ):
1992
1988
df = DataFrame ({"A" : ["a" , "b" , "c" ]})
1993
- result = df .eval ("A == {}" . format ( other ) )
1989
+ result = df .eval (f "A == { other } " )
1994
1990
expected = Series ([False , False , False ], name = "A" )
1995
1991
if _USE_NUMEXPR :
1996
1992
# https://github.com/pandas-dev/pandas/issues/10239
0 commit comments