Skip to content

Commit a53545e

Browse files
jlamborn324datapythonista
authored andcommitted
CLN: replacing str.format with f-strings in several files. #29547 (#30612)
1 parent 8c3883d commit a53545e

File tree

5 files changed

+49
-72
lines changed

5 files changed

+49
-72
lines changed

pandas/tests/io/formats/test_css.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,25 @@ def test_css_side_shorthands(shorthand, expansions):
101101
top, right, bottom, left = expansions
102102

103103
assert_resolves(
104-
"{shorthand}: 1pt".format(shorthand=shorthand),
105-
{top: "1pt", right: "1pt", bottom: "1pt", left: "1pt"},
104+
f"{shorthand}: 1pt", {top: "1pt", right: "1pt", bottom: "1pt", left: "1pt"},
106105
)
107106

108107
assert_resolves(
109-
"{shorthand}: 1pt 4pt".format(shorthand=shorthand),
110-
{top: "1pt", right: "4pt", bottom: "1pt", left: "4pt"},
108+
f"{shorthand}: 1pt 4pt", {top: "1pt", right: "4pt", bottom: "1pt", left: "4pt"},
111109
)
112110

113111
assert_resolves(
114-
"{shorthand}: 1pt 4pt 2pt".format(shorthand=shorthand),
112+
f"{shorthand}: 1pt 4pt 2pt",
115113
{top: "1pt", right: "4pt", bottom: "2pt", left: "4pt"},
116114
)
117115

118116
assert_resolves(
119-
"{shorthand}: 1pt 4pt 2pt 0pt".format(shorthand=shorthand),
117+
f"{shorthand}: 1pt 4pt 2pt 0pt",
120118
{top: "1pt", right: "4pt", bottom: "2pt", left: "0pt"},
121119
)
122120

123121
with tm.assert_produces_warning(CSSWarning):
124-
assert_resolves(
125-
"{shorthand}: 1pt 1pt 1pt 1pt 1pt".format(shorthand=shorthand), {}
126-
)
122+
assert_resolves(f"{shorthand}: 1pt 1pt 1pt 1pt 1pt", {})
127123

128124

129125
@pytest.mark.parametrize(
@@ -174,10 +170,10 @@ def test_css_none_absent(style, equiv):
174170
"size,resolved",
175171
[
176172
("xx-small", "6pt"),
177-
("x-small", "{pt:f}pt".format(pt=7.5)),
178-
("small", "{pt:f}pt".format(pt=9.6)),
173+
("x-small", f"{7.5:f}pt"),
174+
("small", f"{9.6:f}pt"),
179175
("medium", "12pt"),
180-
("large", "{pt:f}pt".format(pt=13.5)),
176+
("large", f"{13.5:f}pt"),
181177
("x-large", "18pt"),
182178
("xx-large", "24pt"),
183179
("8px", "6pt"),
@@ -196,9 +192,7 @@ def test_css_absolute_font_size(size, relative_to, resolved):
196192
else:
197193
inherited = {"font-size": relative_to}
198194
assert_resolves(
199-
"font-size: {size}".format(size=size),
200-
{"font-size": resolved},
201-
inherited=inherited,
195+
f"font-size: {size}", {"font-size": resolved}, inherited=inherited,
202196
)
203197

204198

@@ -224,7 +218,7 @@ def test_css_absolute_font_size(size, relative_to, resolved):
224218
("inherit", "16pt", "16pt"),
225219
("smaller", None, "10pt"),
226220
("smaller", "18pt", "15pt"),
227-
("larger", None, "{pt:f}pt".format(pt=14.4)),
221+
("larger", None, f"{14.4:f}pt"),
228222
("larger", "15pt", "18pt"),
229223
],
230224
)
@@ -234,7 +228,5 @@ def test_css_relative_font_size(size, relative_to, resolved):
234228
else:
235229
inherited = {"font-size": relative_to}
236230
assert_resolves(
237-
"font-size: {size}".format(size=size),
238-
{"font-size": resolved},
239-
inherited=inherited,
231+
f"font-size: {size}", {"font-size": resolved}, inherited=inherited,
240232
)

pandas/tests/io/formats/test_format.py

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,10 @@ def test_repr_truncation_column_size(self):
421421
def test_repr_max_columns_max_rows(self):
422422
term_width, term_height = get_terminal_size()
423423
if term_width < 10 or term_height < 10:
424-
pytest.skip(
425-
"terminal size too small, {0} x {1}".format(term_width, term_height)
426-
)
424+
pytest.skip(f"terminal size too small, {term_width} x {term_height}")
427425

428426
def mkframe(n):
429-
index = ["{i:05d}".format(i=i) for i in range(n)]
427+
index = [f"{i:05d}" for i in range(n)]
430428
return DataFrame(0, index, index)
431429

432430
df6 = mkframe(6)
@@ -667,9 +665,9 @@ def test_to_string_with_formatters(self):
667665
)
668666

669667
formatters = [
670-
("int", lambda x: "0x{x:x}".format(x=x)),
671-
("float", lambda x: "[{x: 4.1f}]".format(x=x)),
672-
("object", lambda x: "-{x!s}-".format(x=x)),
668+
("int", lambda x: f"0x{x:x}"),
669+
("float", lambda x: f"[{x: 4.1f}]"),
670+
("object", lambda x: f"-{x!s}-"),
673671
]
674672
result = df.to_string(formatters=dict(formatters))
675673
result2 = df.to_string(formatters=list(zip(*formatters))[1])
@@ -711,7 +709,7 @@ def format_func(x):
711709

712710
def test_to_string_with_formatters_unicode(self):
713711
df = DataFrame({"c/\u03c3": [1, 2, 3]})
714-
result = df.to_string(formatters={"c/\u03c3": lambda x: "{x}".format(x=x)})
712+
result = df.to_string(formatters={"c/\u03c3": str})
715713
assert result == " c/\u03c3\n" + "0 1\n1 2\n2 3"
716714

717715
def test_east_asian_unicode_false(self):
@@ -1240,7 +1238,7 @@ def test_wide_repr(self):
12401238
set_option("display.expand_frame_repr", False)
12411239
rep_str = repr(df)
12421240

1243-
assert "10 rows x {c} columns".format(c=max_cols - 1) in rep_str
1241+
assert f"10 rows x {max_cols - 1} columns" in rep_str
12441242
set_option("display.expand_frame_repr", True)
12451243
wide_repr = repr(df)
12461244
assert rep_str != wide_repr
@@ -1351,7 +1349,7 @@ def test_long_series(self):
13511349
n = 1000
13521350
s = Series(
13531351
np.random.randint(-50, 50, n),
1354-
index=["s{x:04d}".format(x=x) for x in range(n)],
1352+
index=[f"s{x:04d}" for x in range(n)],
13551353
dtype="int64",
13561354
)
13571355

@@ -1477,9 +1475,7 @@ def test_to_string(self):
14771475
expected = ["A"]
14781476
assert header == expected
14791477

1480-
biggie.to_string(
1481-
columns=["B", "A"], formatters={"A": lambda x: "{x:.1f}".format(x=x)}
1482-
)
1478+
biggie.to_string(columns=["B", "A"], formatters={"A": lambda x: f"{x:.1f}"})
14831479

14841480
biggie.to_string(columns=["B", "A"], float_format=str)
14851481
biggie.to_string(columns=["B", "A"], col_space=12, float_format=str)
@@ -1610,7 +1606,7 @@ def test_to_string_small_float_values(self):
16101606

16111607
result = df.to_string()
16121608
# sadness per above
1613-
if "{x:.4g}".format(x=1.7e8) == "1.7e+008":
1609+
if _three_digit_exp():
16141610
expected = (
16151611
" a\n"
16161612
"0 1.500000e+000\n"
@@ -1922,7 +1918,7 @@ def test_repr_html_long(self):
19221918
long_repr = df._repr_html_()
19231919
assert ".." in long_repr
19241920
assert str(41 + max_rows // 2) not in long_repr
1925-
assert "{h} rows ".format(h=h) in long_repr
1921+
assert f"{h} rows " in long_repr
19261922
assert "2 columns" in long_repr
19271923

19281924
def test_repr_html_float(self):
@@ -1939,7 +1935,7 @@ def test_repr_html_float(self):
19391935
).set_index("idx")
19401936
reg_repr = df._repr_html_()
19411937
assert ".." not in reg_repr
1942-
assert "<td>{val}</td>".format(val=str(40 + h)) in reg_repr
1938+
assert f"<td>{40 + h}</td>" in reg_repr
19431939

19441940
h = max_rows + 1
19451941
df = DataFrame(
@@ -1951,8 +1947,8 @@ def test_repr_html_float(self):
19511947
).set_index("idx")
19521948
long_repr = df._repr_html_()
19531949
assert ".." in long_repr
1954-
assert "<td>{val}</td>".format(val="31") not in long_repr
1955-
assert "{h} rows ".format(h=h) in long_repr
1950+
assert "<td>31</td>" not in long_repr
1951+
assert f"{h} rows " in long_repr
19561952
assert "2 columns" in long_repr
19571953

19581954
def test_repr_html_long_multiindex(self):
@@ -2181,9 +2177,7 @@ def test_to_string(self):
21812177
cp.name = "foo"
21822178
result = cp.to_string(length=True, name=True, dtype=True)
21832179
last_line = result.split("\n")[-1].strip()
2184-
assert last_line == (
2185-
"Freq: B, Name: foo, Length: {cp}, dtype: float64".format(cp=len(cp))
2186-
)
2180+
assert last_line == (f"Freq: B, Name: foo, Length: {len(cp)}, dtype: float64")
21872181

21882182
def test_freq_name_separation(self):
21892183
s = Series(
@@ -2782,7 +2776,7 @@ def test_to_string_na_rep(self):
27822776

27832777
def test_to_string_float_format(self):
27842778
s = pd.Series(range(10), dtype="float64")
2785-
res = s.to_string(float_format=lambda x: "{0:2.1f}".format(x), max_rows=2)
2779+
res = s.to_string(float_format=lambda x: f"{x:2.1f}", max_rows=2)
27862780
exp = "0 0.0\n ..\n9 9.0"
27872781
assert res == exp
27882782

@@ -2807,7 +2801,7 @@ def test_to_string_multindex_header(self):
28072801

28082802

28092803
def _three_digit_exp():
2810-
return "{x:.4g}".format(x=1.7e8) == "1.7e+008"
2804+
return f"{1.7e8:.4g}" == "1.7e+008"
28112805

28122806

28132807
class TestFloatArrayFormatter:

pandas/tests/io/formats/test_style.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def setup_method(self, method):
2424
self.g = lambda x: x
2525

2626
def h(x, foo="bar"):
27-
return pd.Series("color: {foo}".format(foo=foo), index=x.index, name=x.name)
27+
return pd.Series(f"color: {foo}", index=x.index, name=x.name)
2828

2929
self.h = h
3030
self.styler = Styler(self.df)
@@ -278,7 +278,7 @@ def test_numeric_columns(self):
278278

279279
def test_apply_axis(self):
280280
df = pd.DataFrame({"A": [0, 0], "B": [1, 1]})
281-
f = lambda x: ["val: {max}".format(max=x.max()) for v in x]
281+
f = lambda x: [f"val: {x.max()}" for v in x]
282282
result = df.style.apply(f, axis=1)
283283
assert len(result._todo) == 1
284284
assert len(result.ctx) == 0
@@ -362,7 +362,7 @@ def color_negative_red(val):
362362
strings, black otherwise.
363363
"""
364364
color = "red" if val < 0 else "black"
365-
return "color: {color}".format(color=color)
365+
return f"color: {color}"
366366

367367
dic = {
368368
("a", "d"): [-1.12, 2.11],
@@ -1215,13 +1215,9 @@ def test_highlight_max(self):
12151215

12161216
def test_export(self):
12171217
f = lambda x: "color: red" if x > 0 else "color: blue"
1218-
g = (
1219-
lambda x, y, z: "color: {z}".format(z=z)
1220-
if x > 0
1221-
else "color: {z}".format(z=z)
1222-
)
1218+
g = lambda x, z: f"color: {z}" if x > 0 else f"color: {z}"
12231219
style1 = self.styler
1224-
style1.applymap(f).applymap(g, y="a", z="b").highlight_max()
1220+
style1.applymap(f).applymap(g, z="b").highlight_max()
12251221
result = style1.export()
12261222
style2 = self.df.style
12271223
style2.use(result)
@@ -1645,9 +1641,7 @@ def test_hide_columns_mult_levels(self):
16451641

16461642
def test_pipe(self):
16471643
def set_caption_from_template(styler, a, b):
1648-
return styler.set_caption(
1649-
"Dataframe with a = {a} and b = {b}".format(a=a, b=b)
1650-
)
1644+
return styler.set_caption(f"Dataframe with a = {a} and b = {b}")
16511645

16521646
styler = self.df.style.pipe(set_caption_from_template, "A", b="B")
16531647
assert "Dataframe with a = A and b = B" in styler.render()

pandas/tests/io/formats/test_to_csv.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,7 @@ def test_to_csv_compression(self, compression_only, read_infer, to_infer):
495495
compression = compression_only
496496

497497
if compression == "zip":
498-
pytest.skip(
499-
"{compression} is not supported "
500-
"for to_csv".format(compression=compression)
501-
)
498+
pytest.skip(f"{compression} is not supported for to_csv")
502499

503500
# We'll complete file extension subsequently.
504501
filename = "test."

pandas/tests/io/formats/test_to_excel.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -270,13 +270,13 @@ def test_css_to_excel_inherited(css, inherited, expected):
270270
def test_css_to_excel_good_colors(input_color, output_color):
271271
# see gh-18392
272272
css = (
273-
"border-top-color: {color}; "
274-
"border-right-color: {color}; "
275-
"border-bottom-color: {color}; "
276-
"border-left-color: {color}; "
277-
"background-color: {color}; "
278-
"color: {color}"
279-
).format(color=input_color)
273+
f"border-top-color: {input_color}; "
274+
f"border-right-color: {input_color}; "
275+
f"border-bottom-color: {input_color}; "
276+
f"border-left-color: {input_color}; "
277+
f"background-color: {input_color}; "
278+
f"color: {input_color}"
279+
)
280280

281281
expected = dict()
282282

@@ -297,13 +297,13 @@ def test_css_to_excel_good_colors(input_color, output_color):
297297
def test_css_to_excel_bad_colors(input_color):
298298
# see gh-18392
299299
css = (
300-
"border-top-color: {color}; "
301-
"border-right-color: {color}; "
302-
"border-bottom-color: {color}; "
303-
"border-left-color: {color}; "
304-
"background-color: {color}; "
305-
"color: {color}"
306-
).format(color=input_color)
300+
f"border-top-color: {input_color}; "
301+
f"border-right-color: {input_color}; "
302+
f"border-bottom-color: {input_color}; "
303+
f"border-left-color: {input_color}; "
304+
f"background-color: {input_color}; "
305+
f"color: {input_color}"
306+
)
307307

308308
expected = dict()
309309

0 commit comments

Comments
 (0)