Skip to content

Commit 9d66896

Browse files
authored
CLN: f-string formatting (#31868)
1 parent 4181042 commit 9d66896

File tree

10 files changed

+28
-45
lines changed

10 files changed

+28
-45
lines changed

pandas/tests/indexes/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def test_shift(self):
9898

9999
# GH8083 test the base class for shift
100100
idx = self.create_index()
101-
msg = "Not supported for type {}".format(type(idx).__name__)
101+
msg = f"Not supported for type {type(idx).__name__}"
102102
with pytest.raises(NotImplementedError, match=msg):
103103
idx.shift(1)
104104
with pytest.raises(NotImplementedError, match=msg):
@@ -808,7 +808,7 @@ def test_map_dictlike(self, mapper):
808808

809809
index = self.create_index()
810810
if isinstance(index, (pd.CategoricalIndex, pd.IntervalIndex)):
811-
pytest.skip("skipping tests for {}".format(type(index)))
811+
pytest.skip(f"skipping tests for {type(index)}")
812812

813813
identity = mapper(index.values, index)
814814

pandas/tests/indexes/datetimelike.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ def test_str(self):
3636
# test the string repr
3737
idx = self.create_index()
3838
idx.name = "foo"
39-
assert not "length={}".format(len(idx)) in str(idx)
39+
assert not (f"length={len(idx)}" in str(idx))
4040
assert "'foo'" in str(idx)
4141
assert type(idx).__name__ in str(idx)
4242

4343
if hasattr(idx, "tz"):
4444
if idx.tz is not None:
4545
assert idx.tz in str(idx)
4646
if hasattr(idx, "freq"):
47-
assert "freq='{idx.freqstr}'".format(idx=idx) in str(idx)
47+
assert f"freq='{idx.freqstr}'" in str(idx)
4848

4949
def test_view(self):
5050
i = self.create_index()

pandas/tests/indexing/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
def _mklbl(prefix, n):
11-
return ["{prefix}{i}".format(prefix=prefix, i=i) for i in range(n)]
11+
return [f"{prefix}{i}" for i in range(n)]
1212

1313

1414
def _axify(obj, key, axis):
@@ -96,7 +96,7 @@ def setup_method(self, method):
9696
for kind in self._kinds:
9797
d = dict()
9898
for typ in self._typs:
99-
d[typ] = getattr(self, "{kind}_{typ}".format(kind=kind, typ=typ))
99+
d[typ] = getattr(self, f"{kind}_{typ}")
100100

101101
setattr(self, kind, d)
102102

pandas/tests/indexing/test_coercion.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ class TestReplaceSeriesCoercion(CoercionBase):
943943

944944
for tz in ["UTC", "US/Eastern"]:
945945
# to test tz => different tz replacement
946-
key = "datetime64[ns, {0}]".format(tz)
946+
key = f"datetime64[ns, {tz}]"
947947
rep[key] = [
948948
pd.Timestamp("2011-01-01", tz=tz),
949949
pd.Timestamp("2011-01-03", tz=tz),
@@ -1017,9 +1017,7 @@ def test_replace_series(self, how, to_key, from_key):
10171017
):
10181018

10191019
if compat.is_platform_32bit() or compat.is_platform_windows():
1020-
pytest.skip(
1021-
"32-bit platform buggy: {0} -> {1}".format(from_key, to_key)
1022-
)
1020+
pytest.skip(f"32-bit platform buggy: {from_key} -> {to_key}")
10231021

10241022
# Expected: do not downcast by replacement
10251023
exp = pd.Series(self.rep[to_key], index=index, name="yyy", dtype=from_key)

pandas/tests/indexing/test_iloc.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ def test_iloc_getitem_bool(self):
246246
def test_iloc_getitem_bool_diff_len(self, index):
247247
# GH26658
248248
s = Series([1, 2, 3])
249-
msg = "Boolean index has wrong length: {} instead of {}".format(
250-
len(index), len(s)
251-
)
249+
msg = f"Boolean index has wrong length: {len(index)} instead of {len(s)}"
252250
with pytest.raises(IndexError, match=msg):
253251
_ = s.iloc[index]
254252

@@ -612,9 +610,7 @@ def test_iloc_mask(self):
612610
r = expected.get(key)
613611
if r != ans:
614612
raise AssertionError(
615-
"[{key}] does not match [{ans}], received [{r}]".format(
616-
key=key, ans=ans, r=r
617-
)
613+
f"[{key}] does not match [{ans}], received [{r}]"
618614
)
619615

620616
def test_iloc_non_unique_indexing(self):

pandas/tests/indexing/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def __init__(self, value):
514514
self.value = value
515515

516516
def __str__(self) -> str:
517-
return "[{0}]".format(self.value)
517+
return f"[{self.value}]"
518518

519519
__repr__ = __str__
520520

pandas/tests/indexing/test_loc.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,7 @@ def test_getitem_label_list_with_missing(self):
217217
def test_loc_getitem_bool_diff_len(self, index):
218218
# GH26658
219219
s = Series([1, 2, 3])
220-
msg = "Boolean index has wrong length: {} instead of {}".format(
221-
len(index), len(s)
222-
)
220+
msg = f"Boolean index has wrong length: {len(index)} instead of {len(s)}"
223221
with pytest.raises(IndexError, match=msg):
224222
_ = s.loc[index]
225223

@@ -484,12 +482,8 @@ def test_loc_assign_non_ns_datetime(self, unit):
484482
}
485483
)
486484

487-
df.loc[:, unit] = df.loc[:, "timestamp"].values.astype(
488-
"datetime64[{unit}]".format(unit=unit)
489-
)
490-
df["expected"] = df.loc[:, "timestamp"].values.astype(
491-
"datetime64[{unit}]".format(unit=unit)
492-
)
485+
df.loc[:, unit] = df.loc[:, "timestamp"].values.astype(f"datetime64[{unit}]")
486+
df["expected"] = df.loc[:, "timestamp"].values.astype(f"datetime64[{unit}]")
493487
expected = Series(df.loc[:, "expected"], name=unit)
494488
tm.assert_series_equal(df.loc[:, unit], expected)
495489

pandas/tests/series/methods/test_argsort.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_argsort(self, datetime_series):
2727
assert issubclass(argsorted.dtype.type, np.integer)
2828

2929
# GH#2967 (introduced bug in 0.11-dev I think)
30-
s = Series([Timestamp("201301{i:02d}".format(i=i)) for i in range(1, 6)])
30+
s = Series([Timestamp(f"201301{i:02d}") for i in range(1, 6)])
3131
assert s.dtype == "datetime64[ns]"
3232
shifted = s.shift(-1)
3333
assert shifted.dtype == "datetime64[ns]"

pandas/tests/series/test_constructors.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -811,15 +811,15 @@ def test_constructor_dtype_datetime64(self):
811811
expected = Series(values2, index=dates)
812812

813813
for dtype in ["s", "D", "ms", "us", "ns"]:
814-
values1 = dates.view(np.ndarray).astype("M8[{0}]".format(dtype))
814+
values1 = dates.view(np.ndarray).astype(f"M8[{dtype}]")
815815
result = Series(values1, dates)
816816
tm.assert_series_equal(result, expected)
817817

818818
# GH 13876
819819
# coerce to non-ns to object properly
820820
expected = Series(values2, index=dates, dtype=object)
821821
for dtype in ["s", "D", "ms", "us", "ns"]:
822-
values1 = dates.view(np.ndarray).astype("M8[{0}]".format(dtype))
822+
values1 = dates.view(np.ndarray).astype(f"M8[{dtype}]")
823823
result = Series(values1, index=dates, dtype=object)
824824
tm.assert_series_equal(result, expected)
825825

@@ -952,7 +952,7 @@ def test_constructor_with_datetime_tz(self):
952952
def test_construction_to_datetimelike_unit(self, arr_dtype, dtype, unit):
953953
# tests all units
954954
# gh-19223
955-
dtype = "{}[{}]".format(dtype, unit)
955+
dtype = f"{dtype}[{unit}]"
956956
arr = np.array([1, 2, 3], dtype=arr_dtype)
957957
s = Series(arr)
958958
result = s.astype(dtype)
@@ -1347,12 +1347,11 @@ def test_convert_non_ns(self):
13471347
def test_constructor_cant_cast_datetimelike(self, index):
13481348

13491349
# floats are not ok
1350-
msg = "Cannot cast {}.*? to ".format(
1351-
# strip Index to convert PeriodIndex -> Period
1352-
# We don't care whether the error message says
1353-
# PeriodIndex or PeriodArray
1354-
type(index).__name__.rstrip("Index")
1355-
)
1350+
# strip Index to convert PeriodIndex -> Period
1351+
# We don't care whether the error message says
1352+
# PeriodIndex or PeriodArray
1353+
msg = f"Cannot cast {type(index).__name__.rstrip('Index')}.*? to "
1354+
13561355
with pytest.raises(TypeError, match=msg):
13571356
Series(index, dtype=float)
13581357

pandas/tests/tseries/frequencies/test_inference.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def test_infer_freq_delta(base_delta_code_pair, count):
178178
inc = base_delta * count
179179
index = DatetimeIndex([b + inc * j for j in range(3)])
180180

181-
exp_freq = "{count:d}{code}".format(count=count, code=code) if count > 1 else code
181+
exp_freq = f"{count:d}{code}" if count > 1 else code
182182
assert frequencies.infer_freq(index) == exp_freq
183183

184184

@@ -202,13 +202,11 @@ def test_infer_freq_custom(base_delta_code_pair, constructor):
202202

203203

204204
def test_weekly_infer(periods, day):
205-
_check_generated_range("1/1/2000", periods, "W-{day}".format(day=day))
205+
_check_generated_range("1/1/2000", periods, f"W-{day}")
206206

207207

208208
def test_week_of_month_infer(periods, day, count):
209-
_check_generated_range(
210-
"1/1/2000", periods, "WOM-{count}{day}".format(count=count, day=day)
211-
)
209+
_check_generated_range("1/1/2000", periods, f"WOM-{count}{day}")
212210

213211

214212
@pytest.mark.parametrize("freq", ["M", "BM", "BMS"])
@@ -217,14 +215,12 @@ def test_monthly_infer(periods, freq):
217215

218216

219217
def test_quarterly_infer(month, periods):
220-
_check_generated_range("1/1/2000", periods, "Q-{month}".format(month=month))
218+
_check_generated_range("1/1/2000", periods, f"Q-{month}")
221219

222220

223221
@pytest.mark.parametrize("annual", ["A", "BA"])
224222
def test_annually_infer(month, periods, annual):
225-
_check_generated_range(
226-
"1/1/2000", periods, "{annual}-{month}".format(annual=annual, month=month)
227-
)
223+
_check_generated_range("1/1/2000", periods, f"{annual}-{month}")
228224

229225

230226
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)