Skip to content

Commit 4edf938

Browse files
simonjayhawkinsjreback
authored andcommitted
TST/CLN: replace %s formatting syntax with .format in tests (#27324)
1 parent c74a853 commit 4edf938

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+239
-172
lines changed

pandas/tests/arrays/categorical/test_dtypes.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,22 @@ def test_codes_dtypes(self):
9292
result = Categorical(["foo", "bar", "baz"])
9393
assert result.codes.dtype == "int8"
9494

95-
result = Categorical(["foo%05d" % i for i in range(400)])
95+
result = Categorical(["foo{i:05d}".format(i=i) for i in range(400)])
9696
assert result.codes.dtype == "int16"
9797

98-
result = Categorical(["foo%05d" % i for i in range(40000)])
98+
result = Categorical(["foo{i:05d}".format(i=i) for i in range(40000)])
9999
assert result.codes.dtype == "int32"
100100

101101
# adding cats
102102
result = Categorical(["foo", "bar", "baz"])
103103
assert result.codes.dtype == "int8"
104-
result = result.add_categories(["foo%05d" % i for i in range(400)])
104+
result = result.add_categories(["foo{i:05d}".format(i=i) for i in range(400)])
105105
assert result.codes.dtype == "int16"
106106

107107
# removing cats
108-
result = result.remove_categories(["foo%05d" % i for i in range(300)])
108+
result = result.remove_categories(
109+
["foo{i:05d}".format(i=i) for i in range(300)]
110+
)
109111
assert result.codes.dtype == "int8"
110112

111113
@pytest.mark.parametrize("ordered", [True, False])

pandas/tests/arrays/sparse/test_libsparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,6 @@ def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
596596

597597
@pytest.mark.parametrize("opname", ["add", "sub", "mul", "truediv", "floordiv"])
598598
def test_op(self, opname):
599-
sparse_op = getattr(splib, "sparse_%s_float64" % opname)
599+
sparse_op = getattr(splib, "sparse_{opname}_float64".format(opname=opname))
600600
python_op = getattr(operator, opname)
601601
self._op_tests(sparse_op, python_op)

pandas/tests/computation/test_eval.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,16 +736,16 @@ def test_float_truncation(self):
736736

737737
df = pd.DataFrame({"A": [1000000000.0009, 1000000000.0011, 1000000000.0015]})
738738
cutoff = 1000000000.0006
739-
result = df.query("A < %.4f" % cutoff)
739+
result = df.query("A < {cutoff:.4f}".format(cutoff=cutoff))
740740
assert result.empty
741741

742742
cutoff = 1000000000.0010
743-
result = df.query("A > %.4f" % cutoff)
743+
result = df.query("A > {cutoff:.4f}".format(cutoff=cutoff))
744744
expected = df.loc[[1, 2], :]
745745
tm.assert_frame_equal(expected, result)
746746

747747
exact = 1000000000.0011
748-
result = df.query("A == %.4f" % exact)
748+
result = df.query("A == {exact:.4f}".format(exact=exact))
749749
expected = df.loc[[1], :]
750750
tm.assert_frame_equal(expected, result)
751751

pandas/tests/dtypes/test_inference.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ def test_is_scalar_pandas_containers(self):
13491349

13501350
def test_datetimeindex_from_empty_datetime64_array():
13511351
for unit in ["ms", "us", "ns"]:
1352-
idx = DatetimeIndex(np.array([], dtype="datetime64[%s]" % unit))
1352+
idx = DatetimeIndex(np.array([], dtype="datetime64[{unit}]".format(unit=unit)))
13531353
assert len(idx) == 0
13541354

13551355

pandas/tests/frame/test_alter_axes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ def __init__(self, name, color):
342342
self.color = color
343343

344344
def __str__(self):
345-
return "<Thing %r>" % (self.name,)
345+
return "<Thing {self.name!r}>".format(self=self)
346346

347347
# necessary for pretty KeyError
348348
__repr__ = __str__
@@ -419,7 +419,7 @@ def __init__(self, name, color):
419419
self.color = color
420420

421421
def __str__(self):
422-
return "<Thing %r>" % (self.name,)
422+
return "<Thing {self.name!r}>".format(self=self)
423423

424424
thing1 = Thing("One", "red")
425425
thing2 = Thing("Two", "blue")

pandas/tests/frame/test_api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ def test_get_value(self, float_frame):
7474

7575
def test_add_prefix_suffix(self, float_frame):
7676
with_prefix = float_frame.add_prefix("foo#")
77-
expected = pd.Index(["foo#%s" % c for c in float_frame.columns])
77+
expected = pd.Index(["foo#{c}".format(c=c) for c in float_frame.columns])
7878
tm.assert_index_equal(with_prefix.columns, expected)
7979

8080
with_suffix = float_frame.add_suffix("#foo")
81-
expected = pd.Index(["%s#foo" % c for c in float_frame.columns])
81+
expected = pd.Index(["{c}#foo".format(c=c) for c in float_frame.columns])
8282
tm.assert_index_equal(with_suffix.columns, expected)
8383

8484
with_pct_prefix = float_frame.add_prefix("%")
85-
expected = pd.Index(["%{}".format(c) for c in float_frame.columns])
85+
expected = pd.Index(["%{c}".format(c=c) for c in float_frame.columns])
8686
tm.assert_index_equal(with_pct_prefix.columns, expected)
8787

8888
with_pct_suffix = float_frame.add_suffix("%")
89-
expected = pd.Index(["{}%".format(c) for c in float_frame.columns])
89+
expected = pd.Index(["{c}%".format(c=c) for c in float_frame.columns])
9090
tm.assert_index_equal(with_pct_suffix.columns, expected)
9191

9292
def test_get_axis(self, float_frame):

pandas/tests/frame/test_constructors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_constructor_ordereddict(self):
264264
nitems = 100
265265
nums = list(range(nitems))
266266
random.shuffle(nums)
267-
expected = ["A%d" % i for i in nums]
267+
expected = ["A{i:d}".format(i=i) for i in nums]
268268
df = DataFrame(OrderedDict(zip(expected, [[0]] * nitems)))
269269
assert expected == list(df.columns)
270270

pandas/tests/frame/test_query_eval.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,9 @@ def test_date_query_with_non_date(self):
453453

454454
for op in ["<", ">", "<=", ">="]:
455455
with pytest.raises(TypeError):
456-
df.query("dates %s nondate" % op, parser=parser, engine=engine)
456+
df.query(
457+
"dates {op} nondate".format(op=op), parser=parser, engine=engine
458+
)
457459

458460
def test_query_syntax_error(self):
459461
engine, parser = self.engine, self.parser
@@ -688,7 +690,7 @@ def test_inf(self):
688690
ops = "==", "!="
689691
d = dict(zip(ops, (operator.eq, operator.ne)))
690692
for op, f in d.items():
691-
q = "a %s inf" % op
693+
q = "a {op} inf".format(op=op)
692694
expected = df[f(df.a, np.inf)]
693695
result = df.query(q, engine=self.engine, parser=self.parser)
694696
assert_frame_equal(result, expected)

pandas/tests/frame/test_repr_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def test_info_shows_column_dtypes(self):
285285
df.info(buf=buf)
286286
res = buf.getvalue()
287287
for i, dtype in enumerate(dtypes):
288-
name = "%d %d non-null %s" % (i, n, dtype)
288+
name = "{i:d} {n:d} non-null {dtype}".format(i=i, n=n, dtype=dtype)
289289
assert name in res
290290

291291
def test_info_max_cols(self):

pandas/tests/frame/test_timeseries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def test_frame_append_datetime64_col_other_units(self):
223223
ns_dtype = np.dtype("M8[ns]")
224224

225225
for unit in units:
226-
dtype = np.dtype("M8[%s]" % unit)
226+
dtype = np.dtype("M8[{unit}]".format(unit=unit))
227227
vals = np.arange(n, dtype=np.int64).view(dtype)
228228

229229
df = DataFrame({"ints": np.arange(n)}, index=np.arange(n))
@@ -239,7 +239,7 @@ def test_frame_append_datetime64_col_other_units(self):
239239
df["dates"] = np.arange(n, dtype=np.int64).view(ns_dtype)
240240

241241
for unit in units:
242-
dtype = np.dtype("M8[%s]" % unit)
242+
dtype = np.dtype("M8[{unit}]".format(unit=unit))
243243
vals = np.arange(n, dtype=np.int64).view(dtype)
244244

245245
tmp = df.copy()

0 commit comments

Comments
 (0)