Skip to content

Commit 2640d10

Browse files
JMBurleyjreback
authored andcommitted
CLN .format to f-strings for several files (#30442)
1 parent 5a7b5c9 commit 2640d10

File tree

5 files changed

+49
-55
lines changed

5 files changed

+49
-55
lines changed

pandas/tests/computation/test_eval.py

+42-46
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,8 @@
4242
engine,
4343
marks=pytest.mark.skipif(
4444
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}",
4947
),
5048
)
5149
for engine in _engines
@@ -189,9 +187,7 @@ def test_complex_cmp_ops(self, cmp1, cmp2):
189187
rhs_new = _eval_single_bin(lhs, cmp2, rhs, self.engine)
190188
expected = _eval_single_bin(lhs_new, binop, rhs_new, self.engine)
191189

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)"
195191
result = pd.eval(ex, engine=self.engine, parser=self.parser)
196192
self.check_equal(result, expected)
197193

@@ -265,9 +261,9 @@ def check_operands(left, right, cmp_op):
265261
rhs_new = check_operands(mid, rhs, cmp2)
266262

267263
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)"
271267
expected = _eval_single_bin(lhs_new, "&", rhs_new, self.engine)
272268

273269
for ex in (ex1, ex2, ex3):
@@ -276,7 +272,7 @@ def check_operands(left, right, cmp_op):
276272
tm.assert_almost_equal(result, expected)
277273

278274
def check_simple_cmp_op(self, lhs, cmp1, rhs):
279-
ex = "lhs {0} rhs".format(cmp1)
275+
ex = f"lhs {cmp1} rhs"
280276
msg = (
281277
r"only list-like( or dict-like)? objects are allowed to be"
282278
r" passed to (DataFrame\.)?isin\(\), you passed a"
@@ -297,12 +293,12 @@ def check_simple_cmp_op(self, lhs, cmp1, rhs):
297293
self.check_equal(result, expected)
298294

299295
def check_binary_arith_op(self, lhs, arith1, rhs):
300-
ex = "lhs {0} rhs".format(arith1)
296+
ex = f"lhs {arith1} rhs"
301297
result = pd.eval(ex, engine=self.engine, parser=self.parser)
302298
expected = _eval_single_bin(lhs, arith1, rhs, self.engine)
303299

304300
tm.assert_almost_equal(result, expected)
305-
ex = "lhs {0} rhs {0} rhs".format(arith1)
301+
ex = f"lhs {arith1} rhs {arith1} rhs"
306302
result = pd.eval(ex, engine=self.engine, parser=self.parser)
307303
nlhs = _eval_single_bin(lhs, arith1, rhs, self.engine)
308304
self.check_alignment(result, nlhs, rhs, arith1)
@@ -317,25 +313,25 @@ def check_alignment(self, result, nlhs, ghs, op):
317313
else:
318314

319315
# direct numpy comparison
320-
expected = self.ne.evaluate("nlhs {0} ghs".format(op))
316+
expected = self.ne.evaluate(f"nlhs {op} ghs")
321317
tm.assert_numpy_array_equal(result.values, expected)
322318

323319
# modulus, pow, and floor division require special casing
324320

325321
def check_modulus(self, lhs, arith1, rhs):
326-
ex = "lhs {0} rhs".format(arith1)
322+
ex = f"lhs {arith1} rhs"
327323
result = pd.eval(ex, engine=self.engine, parser=self.parser)
328324
expected = lhs % rhs
329325

330326
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")
332328
if isinstance(result, (DataFrame, Series)):
333329
tm.assert_almost_equal(result.values, expected)
334330
else:
335331
tm.assert_almost_equal(result, expected.item())
336332

337333
def check_floor_division(self, lhs, arith1, rhs):
338-
ex = "lhs {0} rhs".format(arith1)
334+
ex = f"lhs {arith1} rhs"
339335

340336
if self.engine == "python":
341337
res = pd.eval(ex, engine=self.engine, parser=self.parser)
@@ -370,7 +366,7 @@ def get_expected_pow_result(self, lhs, rhs):
370366
return expected
371367

372368
def check_pow(self, lhs, arith1, rhs):
373-
ex = "lhs {0} rhs".format(arith1)
369+
ex = f"lhs {arith1} rhs"
374370
expected = self.get_expected_pow_result(lhs, rhs)
375371
result = pd.eval(ex, engine=self.engine, parser=self.parser)
376372

@@ -384,7 +380,7 @@ def check_pow(self, lhs, arith1, rhs):
384380
else:
385381
tm.assert_almost_equal(result, expected)
386382

387-
ex = "(lhs {0} rhs) {0} rhs".format(arith1)
383+
ex = f"(lhs {arith1} rhs) {arith1} rhs"
388384
result = pd.eval(ex, engine=self.engine, parser=self.parser)
389385
expected = self.get_expected_pow_result(
390386
self.get_expected_pow_result(lhs, rhs), rhs
@@ -409,7 +405,7 @@ def check_single_invert_op(self, lhs, cmp1, rhs):
409405

410406
def check_compound_invert_op(self, lhs, cmp1, rhs):
411407
skip_these = ["in", "not in"]
412-
ex = "~(lhs {0} rhs)".format(cmp1)
408+
ex = f"~(lhs {cmp1} rhs)"
413409

414410
msg = (
415411
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):
443439
tm.assert_almost_equal(ev, result)
444440

445441
def ex(self, op, var_name="lhs"):
446-
return "{0}{1}".format(op, var_name)
442+
return f"{op}{var_name}"
447443

448444
def test_frame_invert(self):
449445
expr = self.ex("~")
@@ -733,16 +729,16 @@ def test_float_truncation(self):
733729

734730
df = pd.DataFrame({"A": [1000000000.0009, 1000000000.0011, 1000000000.0015]})
735731
cutoff = 1000000000.0006
736-
result = df.query("A < {cutoff:.4f}".format(cutoff=cutoff))
732+
result = df.query(f"A < {cutoff:.4f}")
737733
assert result.empty
738734

739735
cutoff = 1000000000.0010
740-
result = df.query("A > {cutoff:.4f}".format(cutoff=cutoff))
736+
result = df.query(f"A > {cutoff:.4f}")
741737
expected = df.loc[[1, 2], :]
742738
tm.assert_frame_equal(expected, result)
743739

744740
exact = 1000000000.0011
745-
result = df.query("A == {exact:.4f}".format(exact=exact))
741+
result = df.query(f"A == {exact:.4f}")
746742
expected = df.loc[[1], :]
747743
tm.assert_frame_equal(expected, result)
748744

@@ -781,7 +777,7 @@ def setup_ops(self):
781777
self.unary_ops = "+", "-", "~"
782778

783779
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"
785781
with pytest.raises(NotImplementedError):
786782
pd.eval(ex1, engine=self.engine, parser=self.parser)
787783

@@ -794,7 +790,7 @@ def setup_class(cls):
794790
cls.parser = "python"
795791

796792
def check_modulus(self, lhs, arith1, rhs):
797-
ex = "lhs {0} rhs".format(arith1)
793+
ex = f"lhs {arith1} rhs"
798794
result = pd.eval(ex, engine=self.engine, parser=self.parser)
799795

800796
expected = lhs % rhs
@@ -811,7 +807,7 @@ def check_alignment(self, result, nlhs, ghs, op):
811807
# TypeError, AttributeError: series or frame with scalar align
812808
pass
813809
else:
814-
expected = eval("nlhs {0} ghs".format(op))
810+
expected = eval(f"nlhs {op} ghs")
815811
tm.assert_almost_equal(result, expected)
816812

817813

@@ -840,13 +836,13 @@ class TestTypeCasting:
840836
@pytest.mark.parametrize("dt", [np.float32, np.float64])
841837
def test_binop_typecasting(self, engine, parser, op, dt):
842838
df = tm.makeCustomDataframe(5, 3, data_gen_f=f, dtype=dt)
843-
s = "df {} 3".format(op)
839+
s = f"df {op} 3"
844840
res = pd.eval(s, engine=engine, parser=parser)
845841
assert df.values.dtype == dt
846842
assert res.values.dtype == dt
847843
tm.assert_frame_equal(res, eval(s))
848844

849-
s = "3 {} df".format(op)
845+
s = f"3 {op} df"
850846
res = pd.eval(s, engine=engine, parser=parser)
851847
assert df.values.dtype == dt
852848
assert res.values.dtype == dt
@@ -1013,8 +1009,8 @@ def test_series_frame_commutativity(self, engine, parser):
10131009
index = getattr(df, index_name)
10141010
s = Series(np.random.randn(5), index[:5])
10151011

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"
10181014
if should_warn(df.index, s.index):
10191015
with tm.assert_produces_warning(RuntimeWarning):
10201016
a = pd.eval(lhs, engine=engine, parser=parser)
@@ -1149,9 +1145,9 @@ def test_simple_arith_ops(self):
11491145
ops = self.arith_ops
11501146

11511147
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)"
11551151

11561152
if op in ("in", "not in"):
11571153
msg = "argument of type 'int' is not iterable"
@@ -1176,7 +1172,7 @@ def test_simple_arith_ops(self):
11761172

11771173
def test_simple_bool_ops(self):
11781174
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}"
11801176
res = self.eval(ex)
11811177
exp = eval(ex)
11821178
assert res == exp
@@ -1185,7 +1181,7 @@ def test_bool_ops_with_constants(self):
11851181
for op, lhs, rhs in product(
11861182
expr._bool_ops_syms, ("True", "False"), ("True", "False")
11871183
):
1188-
ex = "{0} {1} {2}".format(lhs, op, rhs)
1184+
ex = f"{lhs} {op} {rhs}"
11891185
res = self.eval(ex)
11901186
exp = eval(ex)
11911187
assert res == exp
@@ -1679,7 +1675,7 @@ def test_bool_ops_with_constants(self):
16791675
for op, lhs, rhs in product(
16801676
expr._bool_ops_syms, ("True", "False"), ("True", "False")
16811677
):
1682-
ex = "{0} {1} {2}".format(lhs, op, rhs)
1678+
ex = f"{lhs} {op} {rhs}"
16831679
if op in ("and", "or"):
16841680
with pytest.raises(NotImplementedError):
16851681
self.eval(ex)
@@ -1690,7 +1686,7 @@ def test_bool_ops_with_constants(self):
16901686

16911687
def test_simple_bool_ops(self):
16921688
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"
16941690
if op in ("and", "or"):
16951691
with pytest.raises(NotImplementedError):
16961692
pd.eval(ex, engine=self.engine, parser=self.parser)
@@ -1742,25 +1738,25 @@ def test_unary_functions(self, unary_fns_for_ne):
17421738
a = df.a
17431739

17441740
for fn in unary_fns_for_ne:
1745-
expr = "{0}(a)".format(fn)
1741+
expr = f"{fn}(a)"
17461742
got = self.eval(expr)
17471743
with np.errstate(all="ignore"):
17481744
expect = getattr(np, fn)(a)
17491745
tm.assert_series_equal(got, expect, check_names=False)
17501746

17511747
def test_floor_and_ceil_functions_raise_error(self, ne_lt_2_6_9, unary_fns_for_ne):
17521748
for fn in ("floor", "ceil"):
1753-
msg = '"{0}" is not a supported function'.format(fn)
1749+
msg = f'"{fn}" is not a supported function'
17541750
with pytest.raises(ValueError, match=msg):
1755-
expr = "{0}(100)".format(fn)
1751+
expr = f"{fn}(100)"
17561752
self.eval(expr)
17571753

17581754
def test_binary_functions(self):
17591755
df = DataFrame({"a": np.random.randn(10), "b": np.random.randn(10)})
17601756
a = df.a
17611757
b = df.b
17621758
for fn in self.binary_fns:
1763-
expr = "{0}(a, b)".format(fn)
1759+
expr = f"{fn}(a, b)"
17641760
got = self.eval(expr)
17651761
with np.errstate(all="ignore"):
17661762
expect = getattr(np, fn)(a, b)
@@ -1971,9 +1967,9 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
19711967
lhs = gen[lhs]() # noqa
19721968
rhs = gen[rhs]() # noqa
19731969

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)"
19771973
for ex in (ex1, ex2, ex3):
19781974
with pytest.raises(NotImplementedError):
19791975
pd.eval(ex, engine=engine, parser=parser)
@@ -1990,7 +1986,7 @@ def test_bool_ops_fails_on_scalars(lhs, cmp, rhs, engine, parser):
19901986
)
19911987
def test_equals_various(other):
19921988
df = DataFrame({"A": ["a", "b", "c"]})
1993-
result = df.eval("A == {}".format(other))
1989+
result = df.eval(f"A == {other}")
19941990
expected = Series([False, False, False], name="A")
19951991
if _USE_NUMEXPR:
19961992
# https://github.com/pandas-dev/pandas/issues/10239

pandas/tests/dtypes/cast/test_infer_dtype.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def test_infer_dtype_from_period(freq, pandas_dtype):
8181
dtype, val = infer_dtype_from_scalar(p, pandas_dtype=pandas_dtype)
8282

8383
if pandas_dtype:
84-
exp_dtype = "period[{0}]".format(freq)
84+
exp_dtype = f"period[{freq}]"
8585
exp_val = p.ordinal
8686
else:
8787
exp_dtype = np.object_
@@ -105,7 +105,7 @@ def test_infer_from_scalar_tz(tz, pandas_dtype):
105105
dtype, val = infer_dtype_from_scalar(dt, pandas_dtype=pandas_dtype)
106106

107107
if pandas_dtype:
108-
exp_dtype = "datetime64[ns, {0}]".format(tz)
108+
exp_dtype = f"datetime64[ns, {tz}]"
109109
exp_val = dt.value
110110
else:
111111
exp_dtype = np.object_

pandas/tests/dtypes/test_dtypes.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def test_dst(self):
305305
@pytest.mark.parametrize("constructor", ["M8", "datetime64"])
306306
def test_parser(self, tz, constructor):
307307
# pr #11245
308-
dtz_str = "{con}[ns, {tz}]".format(con=constructor, tz=tz)
308+
dtz_str = f"{constructor}[ns, {tz}]"
309309
result = DatetimeTZDtype.construct_from_string(dtz_str)
310310
expected = DatetimeTZDtype("ns", tz)
311311
assert result == expected
@@ -635,7 +635,7 @@ def test_equality_generic(self, subtype):
635635
def test_name_repr(self, subtype):
636636
# GH 18980
637637
dtype = IntervalDtype(subtype)
638-
expected = "interval[{subtype}]".format(subtype=subtype)
638+
expected = f"interval[{subtype}]"
639639
assert str(dtype) == expected
640640
assert dtype.name == "interval"
641641

pandas/tests/dtypes/test_inference.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ def test_is_datetime_dtypes(self):
13201320
assert is_datetime64tz_dtype(tsa)
13211321

13221322
for tz in ["US/Eastern", "UTC"]:
1323-
dtype = "datetime64[ns, {}]".format(tz)
1323+
dtype = f"datetime64[ns, {tz}]"
13241324
assert not is_datetime64_dtype(dtype)
13251325
assert is_datetime64tz_dtype(dtype)
13261326
assert is_datetime64_ns_dtype(dtype)
@@ -1414,7 +1414,7 @@ def test_is_scalar_pandas_containers(self):
14141414

14151415
def test_datetimeindex_from_empty_datetime64_array():
14161416
for unit in ["ms", "us", "ns"]:
1417-
idx = DatetimeIndex(np.array([], dtype="datetime64[{unit}]".format(unit=unit)))
1417+
idx = DatetimeIndex(np.array([], dtype=f"datetime64[{unit}]"))
14181418
assert len(idx) == 0
14191419

14201420

pandas/tests/scalar/interval/test_ops.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ def test_overlaps_endpoint(self, start_shift, closed, other_closed):
5959
)
6060
def test_overlaps_invalid_type(self, other):
6161
interval = Interval(0, 1)
62-
msg = "`other` must be an Interval, got {other}".format(
63-
other=type(other).__name__
64-
)
62+
msg = f"`other` must be an Interval, got {type(other).__name__}"
6563
with pytest.raises(TypeError, match=msg):
6664
interval.overlaps(other)

0 commit comments

Comments
 (0)