Skip to content

Commit ee8b856

Browse files
authored
CLN: 29547 replace old string formatting 1 (#31914)
1 parent a4d743e commit ee8b856

File tree

10 files changed

+29
-32
lines changed

10 files changed

+29
-32
lines changed

Diff for: pandas/tests/extension/decimal/test_decimal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def assert_frame_equal(cls, left, right, *args, **kwargs):
9999
check_names=kwargs.get("check_names", True),
100100
check_exact=kwargs.get("check_exact", False),
101101
check_categorical=kwargs.get("check_categorical", True),
102-
obj="{obj}.columns".format(obj=kwargs.get("obj", "DataFrame")),
102+
obj=f"{kwargs.get('obj', 'DataFrame')}.columns",
103103
)
104104

105105
decimals = (left.dtypes == "decimal").index

Diff for: pandas/tests/frame/indexing/test_categorical.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def test_assignment(self):
1414
df = DataFrame(
1515
{"value": np.array(np.random.randint(0, 10000, 100), dtype="int32")}
1616
)
17-
labels = Categorical(
18-
["{0} - {1}".format(i, i + 499) for i in range(0, 10000, 500)]
19-
)
17+
labels = Categorical([f"{i} - {i + 499}" for i in range(0, 10000, 500)])
2018

2119
df = df.sort_values(by=["value"], ascending=True)
2220
s = pd.cut(df.value, range(0, 10500, 500), right=False, labels=labels)
@@ -348,7 +346,7 @@ def test_assigning_ops(self):
348346

349347
def test_functions_no_warnings(self):
350348
df = DataFrame({"value": np.random.randint(0, 100, 20)})
351-
labels = ["{0} - {1}".format(i, i + 9) for i in range(0, 100, 10)]
349+
labels = [f"{i} - {i + 9}" for i in range(0, 100, 10)]
352350
with tm.assert_produces_warning(False):
353351
df["group"] = pd.cut(
354352
df.value, range(0, 105, 10), right=False, labels=labels

Diff for: pandas/tests/frame/methods/test_describe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def test_describe_bool_frame(self):
8686

8787
def test_describe_categorical(self):
8888
df = DataFrame({"value": np.random.randint(0, 10000, 100)})
89-
labels = ["{0} - {1}".format(i, i + 499) for i in range(0, 10000, 500)]
89+
labels = [f"{i} - {i + 499}" for i in range(0, 10000, 500)]
9090
cat_labels = Categorical(labels, labels)
9191

9292
df = df.sort_values(by=["value"], ascending=True)

Diff for: pandas/tests/frame/methods/test_duplicated.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def test_duplicated_do_not_fail_on_wide_dataframes():
2222
# gh-21524
2323
# Given the wide dataframe with a lot of columns
2424
# with different (important!) values
25-
data = {
26-
"col_{0:02d}".format(i): np.random.randint(0, 1000, 30000) for i in range(100)
27-
}
25+
data = {f"col_{i:02d}": np.random.randint(0, 1000, 30000) for i in range(100)}
2826
df = DataFrame(data).T
2927
result = df.duplicated()
3028

Diff for: pandas/tests/frame/methods/test_to_dict.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ def test_to_dict_numeric_names(self):
236236

237237
def test_to_dict_wide(self):
238238
# GH#24939
239-
df = DataFrame({("A_{:d}".format(i)): [i] for i in range(256)})
239+
df = DataFrame({(f"A_{i:d}"): [i] for i in range(256)})
240240
result = df.to_dict("records")[0]
241-
expected = {"A_{:d}".format(i): i for i in range(256)}
241+
expected = {f"A_{i:d}": i for i in range(256)}
242242
assert result == expected
243243

244244
def test_to_dict_orient_dtype(self):

Diff for: pandas/tests/frame/test_alter_axes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,9 @@ class Thing(frozenset):
382382
# need to stabilize repr for KeyError (due to random order in sets)
383383
def __repr__(self) -> str:
384384
tmp = sorted(self)
385+
joined_reprs = ", ".join(map(repr, tmp))
385386
# double curly brace prints one brace in format string
386-
return "frozenset({{{}}})".format(", ".join(map(repr, tmp)))
387+
return f"frozenset({{{joined_reprs}}})"
387388

388389
thing1 = Thing(["One", "red"])
389390
thing2 = Thing(["Two", "blue"])

Diff for: pandas/tests/frame/test_api.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ def test_get_value(self, float_frame):
4646

4747
def test_add_prefix_suffix(self, float_frame):
4848
with_prefix = float_frame.add_prefix("foo#")
49-
expected = pd.Index(["foo#{c}".format(c=c) for c in float_frame.columns])
49+
expected = pd.Index([f"foo#{c}" for c in float_frame.columns])
5050
tm.assert_index_equal(with_prefix.columns, expected)
5151

5252
with_suffix = float_frame.add_suffix("#foo")
53-
expected = pd.Index(["{c}#foo".format(c=c) for c in float_frame.columns])
53+
expected = pd.Index([f"{c}#foo" for c in float_frame.columns])
5454
tm.assert_index_equal(with_suffix.columns, expected)
5555

5656
with_pct_prefix = float_frame.add_prefix("%")
57-
expected = pd.Index(["%{c}".format(c=c) for c in float_frame.columns])
57+
expected = pd.Index([f"%{c}" for c in float_frame.columns])
5858
tm.assert_index_equal(with_pct_prefix.columns, expected)
5959

6060
with_pct_suffix = float_frame.add_suffix("%")
61-
expected = pd.Index(["{c}%".format(c=c) for c in float_frame.columns])
61+
expected = pd.Index([f"{c}%" for c in float_frame.columns])
6262
tm.assert_index_equal(with_pct_suffix.columns, expected)
6363

6464
def test_get_axis(self, float_frame):

Diff for: pandas/tests/frame/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def test_constructor_ordereddict(self):
278278
nitems = 100
279279
nums = list(range(nitems))
280280
random.shuffle(nums)
281-
expected = ["A{i:d}".format(i=i) for i in nums]
281+
expected = [f"A{i:d}" for i in nums]
282282
df = DataFrame(OrderedDict(zip(expected, [[0]] * nitems)))
283283
assert expected == list(df.columns)
284284

Diff for: pandas/tests/frame/test_dtypes.py

+14-14
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ def test_astype_categorical(self, dtype):
702702
@pytest.mark.parametrize("cls", [CategoricalDtype, DatetimeTZDtype, IntervalDtype])
703703
def test_astype_categoricaldtype_class_raises(self, cls):
704704
df = DataFrame({"A": ["a", "a", "b", "c"]})
705-
xpr = "Expected an instance of {}".format(cls.__name__)
705+
xpr = f"Expected an instance of {cls.__name__}"
706706
with pytest.raises(TypeError, match=xpr):
707707
df.astype({"A": cls})
708708

@@ -827,7 +827,7 @@ def test_df_where_change_dtype(self):
827827
def test_astype_from_datetimelike_to_objectt(self, dtype, unit):
828828
# tests astype to object dtype
829829
# gh-19223 / gh-12425
830-
dtype = "{}[{}]".format(dtype, unit)
830+
dtype = f"{dtype}[{unit}]"
831831
arr = np.array([[1, 2, 3]], dtype=dtype)
832832
df = DataFrame(arr)
833833
result = df.astype(object)
@@ -844,7 +844,7 @@ def test_astype_from_datetimelike_to_objectt(self, dtype, unit):
844844
def test_astype_to_datetimelike_unit(self, arr_dtype, dtype, unit):
845845
# tests all units from numeric origination
846846
# gh-19223 / gh-12425
847-
dtype = "{}[{}]".format(dtype, unit)
847+
dtype = f"{dtype}[{unit}]"
848848
arr = np.array([[1, 2, 3]], dtype=arr_dtype)
849849
df = DataFrame(arr)
850850
result = df.astype(dtype)
@@ -856,7 +856,7 @@ def test_astype_to_datetimelike_unit(self, arr_dtype, dtype, unit):
856856
def test_astype_to_datetime_unit(self, unit):
857857
# tests all units from datetime origination
858858
# gh-19223
859-
dtype = "M8[{}]".format(unit)
859+
dtype = f"M8[{unit}]"
860860
arr = np.array([[1, 2, 3]], dtype=dtype)
861861
df = DataFrame(arr)
862862
result = df.astype(dtype)
@@ -868,7 +868,7 @@ def test_astype_to_datetime_unit(self, unit):
868868
def test_astype_to_timedelta_unit_ns(self, unit):
869869
# preserver the timedelta conversion
870870
# gh-19223
871-
dtype = "m8[{}]".format(unit)
871+
dtype = f"m8[{unit}]"
872872
arr = np.array([[1, 2, 3]], dtype=dtype)
873873
df = DataFrame(arr)
874874
result = df.astype(dtype)
@@ -880,7 +880,7 @@ def test_astype_to_timedelta_unit_ns(self, unit):
880880
def test_astype_to_timedelta_unit(self, unit):
881881
# coerce to float
882882
# gh-19223
883-
dtype = "m8[{}]".format(unit)
883+
dtype = f"m8[{unit}]"
884884
arr = np.array([[1, 2, 3]], dtype=dtype)
885885
df = DataFrame(arr)
886886
result = df.astype(dtype)
@@ -892,21 +892,21 @@ def test_astype_to_timedelta_unit(self, unit):
892892
def test_astype_to_incorrect_datetimelike(self, unit):
893893
# trying to astype a m to a M, or vice-versa
894894
# gh-19224
895-
dtype = "M8[{}]".format(unit)
896-
other = "m8[{}]".format(unit)
895+
dtype = f"M8[{unit}]"
896+
other = f"m8[{unit}]"
897897

898898
df = DataFrame(np.array([[1, 2, 3]], dtype=dtype))
899899
msg = (
900-
r"cannot astype a datetimelike from \[datetime64\[ns\]\] to "
901-
r"\[timedelta64\[{}\]\]"
902-
).format(unit)
900+
fr"cannot astype a datetimelike from \[datetime64\[ns\]\] to "
901+
fr"\[timedelta64\[{unit}\]\]"
902+
)
903903
with pytest.raises(TypeError, match=msg):
904904
df.astype(other)
905905

906906
msg = (
907-
r"cannot astype a timedelta from \[timedelta64\[ns\]\] to "
908-
r"\[datetime64\[{}\]\]"
909-
).format(unit)
907+
fr"cannot astype a timedelta from \[timedelta64\[ns\]\] to "
908+
fr"\[datetime64\[{unit}\]\]"
909+
)
910910
df = DataFrame(np.array([[1, 2, 3]], dtype=other))
911911
with pytest.raises(TypeError, match=msg):
912912
df.astype(dtype)

Diff for: pandas/tests/frame/test_join.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def test_join_overlap(float_frame):
161161

162162

163163
def test_join_period_index(frame_with_period_index):
164-
other = frame_with_period_index.rename(columns=lambda x: "{key}{key}".format(key=x))
164+
other = frame_with_period_index.rename(columns=lambda key: f"{key}{key}")
165165

166166
joined_values = np.concatenate([frame_with_period_index.values] * 2, axis=1)
167167

0 commit comments

Comments
 (0)