Skip to content

Commit 207ab74

Browse files
srinivasreddyjreback
authored andcommitted
CLN: Update .format(...) strings to f-expressions (#29571)
1 parent 1746099 commit 207ab74

File tree

14 files changed

+32
-32
lines changed

14 files changed

+32
-32
lines changed

asv_bench/benchmarks/categoricals.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ValueCounts:
8484

8585
def setup(self, dropna):
8686
n = 5 * 10 ** 5
87-
arr = ["s{:04d}".format(i) for i in np.random.randint(0, n // 10, size=n)]
87+
arr = [f"s{i:04d}" for i in np.random.randint(0, n // 10, size=n)]
8888
self.ts = pd.Series(arr).astype("category")
8989

9090
def time_value_counts(self, dropna):
@@ -102,7 +102,7 @@ def time_rendering(self):
102102
class SetCategories:
103103
def setup(self):
104104
n = 5 * 10 ** 5
105-
arr = ["s{:04d}".format(i) for i in np.random.randint(0, n // 10, size=n)]
105+
arr = [f"s{i:04d}" for i in np.random.randint(0, n // 10, size=n)]
106106
self.ts = pd.Series(arr).astype("category")
107107

108108
def time_set_categories(self):
@@ -112,7 +112,7 @@ def time_set_categories(self):
112112
class RemoveCategories:
113113
def setup(self):
114114
n = 5 * 10 ** 5
115-
arr = ["s{:04d}".format(i) for i in np.random.randint(0, n // 10, size=n)]
115+
arr = [f"s{i:04d}" for i in np.random.randint(0, n // 10, size=n)]
116116
self.ts = pd.Series(arr).astype("category")
117117

118118
def time_remove_categories(self):
@@ -166,7 +166,7 @@ def setup(self, dtype):
166166
sample_size = 100
167167
arr = [i for i in np.random.randint(0, n // 10, size=n)]
168168
if dtype == "object":
169-
arr = ["s{:04d}".format(i) for i in arr]
169+
arr = [f"s{i:04d}" for i in arr]
170170
self.sample = np.random.choice(arr, sample_size)
171171
self.series = pd.Series(arr).astype("category")
172172

@@ -225,7 +225,7 @@ def setup(self, index):
225225
elif index == "non_monotonic":
226226
self.data = pd.Categorical.from_codes([0, 1, 2] * N, categories=categories)
227227
else:
228-
raise ValueError("Invalid index param: {}".format(index))
228+
raise ValueError(f"Invalid index param: {index}")
229229

230230
self.scalar = 10000
231231
self.list = list(range(10000))

asv_bench/benchmarks/gil.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,11 @@ def setup(self, dtype):
250250
np.random.randn(rows, cols), index=date_range("1/1/2000", periods=rows)
251251
),
252252
"object": DataFrame(
253-
"foo",
254-
index=range(rows),
255-
columns=["object%03d".format(i) for i in range(5)],
253+
"foo", index=range(rows), columns=["object%03d" for _ in range(5)]
256254
),
257255
}
258256

259-
self.fname = "__test_{}__.csv".format(dtype)
257+
self.fname = f"__test_{dtype}__.csv"
260258
df = data[dtype]
261259
df.to_csv(self.fname)
262260

asv_bench/benchmarks/index_object.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Indexing:
146146

147147
def setup(self, dtype):
148148
N = 10 ** 6
149-
self.idx = getattr(tm, "make{}Index".format(dtype))(N)
149+
self.idx = getattr(tm, f"make{dtype}Index")(N)
150150
self.array_mask = (np.arange(N) % 3) == 0
151151
self.series_mask = Series(self.array_mask)
152152
self.sorted = self.idx.sort_values()

asv_bench/benchmarks/io/csv.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def setup(self, sep, thousands):
202202
data = np.random.randn(N, K) * np.random.randint(100, 10000, (N, K))
203203
df = DataFrame(data)
204204
if thousands is not None:
205-
fmt = ":{}".format(thousands)
205+
fmt = f":{thousands}"
206206
fmt = "{" + fmt + "}"
207207
df = df.applymap(lambda x: fmt.format(x))
208208
df.to_csv(self.fname, sep=sep)
@@ -231,7 +231,7 @@ def setup(self, sep, decimal, float_precision):
231231
floats = [
232232
"".join(random.choice(string.digits) for _ in range(28)) for _ in range(15)
233233
]
234-
rows = sep.join(["0{}".format(decimal) + "{}"] * 3) + "\n"
234+
rows = sep.join([f"0{decimal}" + "{}"] * 3) + "\n"
235235
data = rows * 5
236236
data = data.format(*floats) * 200 # 1000 x 3 strings csv
237237
self.StringIO_input = StringIO(data)
@@ -309,9 +309,7 @@ class ReadCSVCachedParseDates(StringIORewind):
309309
param_names = ["do_cache"]
310310

311311
def setup(self, do_cache):
312-
data = (
313-
"\n".join("10/{}".format(year) for year in range(2000, 2100)) + "\n"
314-
) * 10
312+
data = ("\n".join(f"10/{year}" for year in range(2000, 2100)) + "\n") * 10
315313
self.StringIO_input = StringIO(data)
316314

317315
def time_read_csv_cached(self, do_cache):
@@ -336,7 +334,7 @@ class ReadCSVMemoryGrowth(BaseIO):
336334
def setup(self):
337335
with open(self.fname, "w") as f:
338336
for i in range(self.num_rows):
339-
f.write("{i}\n".format(i=i))
337+
f.write(f"{i}\n")
340338

341339
def mem_parser_chunks(self):
342340
# see gh-24805.

asv_bench/benchmarks/io/excel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def _generate_dataframe():
1414
C = 5
1515
df = DataFrame(
1616
np.random.randn(N, C),
17-
columns=["float{}".format(i) for i in range(C)],
17+
columns=[f"float{i}" for i in range(C)],
1818
index=date_range("20000101", periods=N, freq="H"),
1919
)
2020
df["object"] = tm.makeStringIndex(N)

asv_bench/benchmarks/io/hdf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def setup(self, format):
115115
C = 5
116116
self.df = DataFrame(
117117
np.random.randn(N, C),
118-
columns=["float{}".format(i) for i in range(C)],
118+
columns=[f"float{i}" for i in range(C)],
119119
index=date_range("20000101", periods=N, freq="H"),
120120
)
121121
self.df["object"] = tm.makeStringIndex(N)

asv_bench/benchmarks/io/json.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def setup(self, orient, index):
2020
}
2121
df = DataFrame(
2222
np.random.randn(N, 5),
23-
columns=["float_{}".format(i) for i in range(5)],
23+
columns=[f"float_{i}" for i in range(5)],
2424
index=indexes[index],
2525
)
2626
df.to_json(self.fname, orient=orient)
@@ -43,7 +43,7 @@ def setup(self, index):
4343
}
4444
df = DataFrame(
4545
np.random.randn(N, 5),
46-
columns=["float_{}".format(i) for i in range(5)],
46+
columns=[f"float_{i}" for i in range(5)],
4747
index=indexes[index],
4848
)
4949
df.to_json(self.fname, orient="records", lines=True)

asv_bench/benchmarks/io/msgpack.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def setup(self):
1515
C = 5
1616
self.df = DataFrame(
1717
np.random.randn(N, C),
18-
columns=["float{}".format(i) for i in range(C)],
18+
columns=[f"float{i}" for i in range(C)],
1919
index=date_range("20000101", periods=N, freq="H"),
2020
)
2121
self.df["object"] = tm.makeStringIndex(N)

asv_bench/benchmarks/io/pickle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def setup(self):
1313
C = 5
1414
self.df = DataFrame(
1515
np.random.randn(N, C),
16-
columns=["float{}".format(i) for i in range(C)],
16+
columns=[f"float{i}" for i in range(C)],
1717
index=date_range("20000101", periods=N, freq="H"),
1818
)
1919
self.df["object"] = tm.makeStringIndex(N)

asv_bench/benchmarks/io/sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setup(self, connection):
1919
"sqlite": sqlite3.connect(":memory:"),
2020
}
2121
self.table_name = "test_type"
22-
self.query_all = "SELECT * FROM {}".format(self.table_name)
22+
self.query_all = f"SELECT * FROM {self.table_name}"
2323
self.con = con[connection]
2424
self.df = DataFrame(
2525
{
@@ -58,7 +58,7 @@ def setup(self, connection, dtype):
5858
"sqlite": sqlite3.connect(":memory:"),
5959
}
6060
self.table_name = "test_type"
61-
self.query_col = "SELECT {} FROM {}".format(dtype, self.table_name)
61+
self.query_col = f"SELECT {dtype} FROM {self.table_name}"
6262
self.con = con[connection]
6363
self.df = DataFrame(
6464
{

asv_bench/benchmarks/io/stata.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setup(self, convert_dates):
1717
C = self.C = 5
1818
self.df = DataFrame(
1919
np.random.randn(N, C),
20-
columns=["float{}".format(i) for i in range(C)],
20+
columns=[f"float{i}" for i in range(C)],
2121
index=date_range("20000101", periods=N, freq="H"),
2222
)
2323
self.df["object"] = tm.makeStringIndex(self.N)
@@ -47,7 +47,7 @@ def setup(self, convert_dates):
4747
for i in range(10):
4848
missing_data = np.random.randn(self.N)
4949
missing_data[missing_data < 0] = np.nan
50-
self.df["missing_{0}".format(i)] = missing_data
50+
self.df[f"missing_{i}"] = missing_data
5151
self.df.to_stata(self.fname, self.convert_dates)
5252

5353

asv_bench/benchmarks/timedelta.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def setup(self):
1414
self.str_days = []
1515
self.str_seconds = []
1616
for i in self.ints:
17-
self.str_days.append("{0} days".format(i))
18-
self.str_seconds.append("00:00:{0:02d}".format(i))
17+
self.str_days.append(f"{i} days")
18+
self.str_seconds.append(f"00:00:{i:02d}")
1919

2020
def time_convert_int(self):
2121
to_timedelta(self.ints, unit="s")
@@ -34,7 +34,7 @@ class ToTimedeltaErrors:
3434

3535
def setup(self, errors):
3636
ints = np.random.randint(0, 60, size=10000)
37-
self.arr = ["{0} days".format(i) for i in ints]
37+
self.arr = [f"{i} days" for i in ints]
3838
self.arr[-1] = "apple"
3939

4040
def time_convert(self, errors):

pandas/core/groupby/grouper.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,11 @@ def __init__(
288288
if self.name is None:
289289
self.name = index.names[level]
290290

291-
self.grouper, self._codes, self._group_index = index._get_grouper_for_level( # noqa: E501
291+
(
292+
self.grouper,
293+
self._codes,
294+
self._group_index,
295+
) = index._get_grouper_for_level( # noqa: E501
292296
self.grouper, level
293297
)
294298

pandas/io/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def _is_url(url) -> bool:
109109

110110

111111
def _expand_user(
112-
filepath_or_buffer: FilePathOrBuffer[AnyStr]
112+
filepath_or_buffer: FilePathOrBuffer[AnyStr],
113113
) -> FilePathOrBuffer[AnyStr]:
114114
"""Return the argument with an initial component of ~ or ~user
115115
replaced by that user's home directory.
@@ -139,7 +139,7 @@ def _validate_header_arg(header) -> None:
139139

140140

141141
def _stringify_path(
142-
filepath_or_buffer: FilePathOrBuffer[AnyStr]
142+
filepath_or_buffer: FilePathOrBuffer[AnyStr],
143143
) -> FilePathOrBuffer[AnyStr]:
144144
"""Attempt to convert a path-like object to a string.
145145

0 commit comments

Comments
 (0)