Skip to content

Commit e31ab6d

Browse files
committed
CLN: replace variations of "..." + str(...) with f-strings
This was done by hand.
1 parent 588a6f5 commit e31ab6d

File tree

7 files changed

+41
-96
lines changed

7 files changed

+41
-96
lines changed

pandas/_testing/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ def makeCustomIndex(
569569

570570
if names is True:
571571
# build default names
572-
names = [prefix + str(i) for i in range(nlevels)]
572+
names = [f"{prefix}{i}" for i in range(nlevels)]
573573
if names is False:
574574
# pass None to index constructor for no name
575575
names = None

pandas/core/dtypes/dtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ def __new__(cls, subtype=None, closed: str_type | None = None):
11341134
)
11351135
raise TypeError(msg)
11361136

1137-
key = str(subtype) + str(closed)
1137+
key = f"{subtype}{closed}"
11381138
try:
11391139
return cls._cache_dtypes[key]
11401140
except KeyError:

pandas/core/interchange/buffer.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,9 @@ def __dlpack_device__(self) -> tuple[DlpackDeviceType, int | None]:
6464
return (DlpackDeviceType.CPU, None)
6565

6666
def __repr__(self) -> str:
67-
return (
68-
"PandasBuffer("
69-
+ str(
70-
{
71-
"bufsize": self.bufsize,
72-
"ptr": self.ptr,
73-
"device": self.__dlpack_device__()[0].name,
74-
}
75-
)
76-
+ ")"
77-
)
67+
spec = {
68+
"bufsize": self.bufsize,
69+
"ptr": self.ptr,
70+
"device": self.__dlpack_device__()[0].name,
71+
}
72+
return f"PandasBuffer({spec})"

pandas/core/ops/docstrings.py

+14-56
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ def make_flex_doc(op_name: str, typ: str) -> str:
9999
f 1.0
100100
dtype: float64"""
101101

102-
_add_example_SERIES = (
103-
_common_examples_algebra_SERIES
104-
+ """
102+
_add_example_SERIES = f"""{_common_examples_algebra_SERIES}
105103
>>> a.add(b, fill_value=0)
106104
a 2.0
107105
b 1.0
@@ -110,11 +108,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
110108
e NaN
111109
dtype: float64
112110
"""
113-
)
114111

115-
_sub_example_SERIES = (
116-
_common_examples_algebra_SERIES
117-
+ """
112+
_sub_example_SERIES = f"""{_common_examples_algebra_SERIES}
118113
>>> a.subtract(b, fill_value=0)
119114
a 0.0
120115
b 1.0
@@ -123,11 +118,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
123118
e NaN
124119
dtype: float64
125120
"""
126-
)
127121

128-
_mul_example_SERIES = (
129-
_common_examples_algebra_SERIES
130-
+ """
122+
_mul_example_SERIES = f"""{_common_examples_algebra_SERIES}
131123
>>> a.multiply(b, fill_value=0)
132124
a 1.0
133125
b 0.0
@@ -136,11 +128,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
136128
e NaN
137129
dtype: float64
138130
"""
139-
)
140131

141-
_div_example_SERIES = (
142-
_common_examples_algebra_SERIES
143-
+ """
132+
_div_example_SERIES = f"""{_common_examples_algebra_SERIES}
144133
>>> a.divide(b, fill_value=0)
145134
a 1.0
146135
b inf
@@ -149,11 +138,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
149138
e NaN
150139
dtype: float64
151140
"""
152-
)
153141

154-
_floordiv_example_SERIES = (
155-
_common_examples_algebra_SERIES
156-
+ """
142+
_floordiv_example_SERIES = f"""{_common_examples_algebra_SERIES}
157143
>>> a.floordiv(b, fill_value=0)
158144
a 1.0
159145
b NaN
@@ -162,11 +148,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
162148
e NaN
163149
dtype: float64
164150
"""
165-
)
166151

167-
_divmod_example_SERIES = (
168-
_common_examples_algebra_SERIES
169-
+ """
152+
_divmod_example_SERIES = f"""{_common_examples_algebra_SERIES}
170153
>>> a.divmod(b, fill_value=0)
171154
(a 1.0
172155
b NaN
@@ -181,11 +164,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
181164
e NaN
182165
dtype: float64)
183166
"""
184-
)
185167

186-
_mod_example_SERIES = (
187-
_common_examples_algebra_SERIES
188-
+ """
168+
_mod_example_SERIES = f"""{_common_examples_algebra_SERIES}
189169
>>> a.mod(b, fill_value=0)
190170
a 0.0
191171
b NaN
@@ -194,10 +174,7 @@ def make_flex_doc(op_name: str, typ: str) -> str:
194174
e NaN
195175
dtype: float64
196176
"""
197-
)
198-
_pow_example_SERIES = (
199-
_common_examples_algebra_SERIES
200-
+ """
177+
_pow_example_SERIES = f"""{_common_examples_algebra_SERIES}
201178
>>> a.pow(b, fill_value=0)
202179
a 1.0
203180
b 1.0
@@ -206,11 +183,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
206183
e NaN
207184
dtype: float64
208185
"""
209-
)
210186

211-
_ne_example_SERIES = (
212-
_common_examples_algebra_SERIES
213-
+ """
187+
_ne_example_SERIES = f"""{_common_examples_algebra_SERIES}
214188
>>> a.ne(b, fill_value=0)
215189
a False
216190
b True
@@ -219,11 +193,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
219193
e True
220194
dtype: bool
221195
"""
222-
)
223196

224-
_eq_example_SERIES = (
225-
_common_examples_algebra_SERIES
226-
+ """
197+
_eq_example_SERIES = f"""{_common_examples_algebra_SERIES}
227198
>>> a.eq(b, fill_value=0)
228199
a True
229200
b False
@@ -232,11 +203,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
232203
e False
233204
dtype: bool
234205
"""
235-
)
236206

237-
_lt_example_SERIES = (
238-
_common_examples_comparison_SERIES
239-
+ """
207+
_lt_example_SERIES = f"""{_common_examples_comparison_SERIES}
240208
>>> a.lt(b, fill_value=0)
241209
a False
242210
b False
@@ -246,11 +214,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
246214
f True
247215
dtype: bool
248216
"""
249-
)
250217

251-
_le_example_SERIES = (
252-
_common_examples_comparison_SERIES
253-
+ """
218+
_le_example_SERIES = f"""{_common_examples_comparison_SERIES}
254219
>>> a.le(b, fill_value=0)
255220
a False
256221
b True
@@ -260,11 +225,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
260225
f True
261226
dtype: bool
262227
"""
263-
)
264228

265-
_gt_example_SERIES = (
266-
_common_examples_comparison_SERIES
267-
+ """
229+
_gt_example_SERIES = f"""{_common_examples_comparison_SERIES}
268230
>>> a.gt(b, fill_value=0)
269231
a True
270232
b False
@@ -274,11 +236,8 @@ def make_flex_doc(op_name: str, typ: str) -> str:
274236
f False
275237
dtype: bool
276238
"""
277-
)
278239

279-
_ge_example_SERIES = (
280-
_common_examples_comparison_SERIES
281-
+ """
240+
_ge_example_SERIES = f"""{_common_examples_comparison_SERIES}
282241
>>> a.ge(b, fill_value=0)
283242
a True
284243
b True
@@ -288,7 +247,6 @@ def make_flex_doc(op_name: str, typ: str) -> str:
288247
f False
289248
dtype: bool
290249
"""
291-
)
292250

293251
_returns_series = """Series\n The result of the operation."""
294252

pandas/io/formats/style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2625,7 +2625,7 @@ def set_table_styles(
26252625

26262626
table_styles = [
26272627
{
2628-
"selector": str(s["selector"]) + idf + str(idx),
2628+
"selector": f"{s['selector']}{idf}{idx}",
26292629
"props": maybe_convert_css_to_tuples(s["props"]),
26302630
}
26312631
for key, styles in table_styles.items()

pandas/io/sql.py

+8-16
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ def get_engine(engine: str) -> BaseEngine:
13501350
try:
13511351
return engine_class()
13521352
except ImportError as err:
1353-
error_msgs += "\n - " + str(err)
1353+
error_msgs += f"\n - {err}"
13541354

13551355
raise ImportError(
13561356
"Unable to find a usable engine; "
@@ -1911,7 +1911,7 @@ def _create_table_setup(self):
19111911
escape = _get_valid_sqlite_name
19121912

19131913
create_tbl_stmts = [
1914-
escape(cname) + " " + ctype for cname, ctype, _ in column_names_and_types
1914+
f"{escape(cname)} {ctype}" for cname, ctype, _ in column_names_and_types
19151915
]
19161916

19171917
if self.keys is not None and len(self.keys):
@@ -1924,30 +1924,22 @@ def _create_table_setup(self):
19241924
f"CONSTRAINT {self.name}_pk PRIMARY KEY ({cnames_br})"
19251925
)
19261926
if self.schema:
1927-
schema_name = self.schema + "."
1927+
schema_name = f"{self.schema}."
19281928
else:
19291929
schema_name = ""
1930+
create_tbl_stmts_str = ",\n ".join(create_tbl_stmts)
19301931
create_stmts = [
1931-
"CREATE TABLE "
1932-
+ schema_name
1933-
+ escape(self.name)
1934-
+ " (\n"
1935-
+ ",\n ".join(create_tbl_stmts)
1936-
+ "\n)"
1932+
f"CREATE TABLE {schema_name}{escape(self.name)} "
1933+
f"(\n{create_tbl_stmts_str}\n)"
19371934
]
19381935

19391936
ix_cols = [cname for cname, _, is_index in column_names_and_types if is_index]
19401937
if len(ix_cols):
19411938
cnames = "_".join(ix_cols)
19421939
cnames_br = ",".join([escape(c) for c in ix_cols])
1940+
ix_name = f"ix_{self.name}_{cnames}"
19431941
create_stmts.append(
1944-
"CREATE INDEX "
1945-
+ escape("ix_" + self.name + "_" + cnames)
1946-
+ "ON "
1947-
+ escape(self.name)
1948-
+ " ("
1949-
+ cnames_br
1950-
+ ")"
1942+
f"CREATE INDEX {escape(ix_name)} ON {escape(self.name)} ({cnames_br})"
19511943
)
19521944

19531945
return create_stmts

pandas/io/stata.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ class StataMissingValue:
864864
# Conversion to long to avoid hash issues on 32 bit platforms #8968
865865
MISSING_VALUES[b] = "."
866866
for i in range(1, 27):
867-
MISSING_VALUES[i + b] = "." + chr(96 + i)
867+
MISSING_VALUES[i + b] = f".{chr(96 + i)}"
868868

869869
float32_base: bytes = b"\x00\x00\x00\x7f"
870870
increment: int = struct.unpack("<i", b"\x00\x08\x00\x00")[0]
@@ -973,7 +973,7 @@ def __init__(self) -> None:
973973
# with a label, but the underlying variable is -127 to 100
974974
# we're going to drop the label and cast to int
975975
self.DTYPE_MAP = dict(
976-
list(zip(range(1, 245), [np.dtype("a" + str(i)) for i in range(1, 245)]))
976+
list(zip(range(1, 245), [np.dtype(f"a{i}") for i in range(1, 245)]))
977977
+ [
978978
(251, np.dtype(np.int8)),
979979
(252, np.dtype(np.int16)),
@@ -1502,9 +1502,9 @@ def _setup_dtype(self) -> np.dtype:
15021502
for i, typ in enumerate(self.typlist):
15031503
if typ in self.NUMPY_TYPE_MAP:
15041504
typ = cast(str, typ) # only strs in NUMPY_TYPE_MAP
1505-
dtypes.append(("s" + str(i), self.byteorder + self.NUMPY_TYPE_MAP[typ]))
1505+
dtypes.append((f"s{i}", self.byteorder + self.NUMPY_TYPE_MAP[typ]))
15061506
else:
1507-
dtypes.append(("s" + str(i), "S" + str(typ)))
1507+
dtypes.append((f"s{i}", f"S{typ}"))
15081508
self._dtype = np.dtype(dtypes)
15091509

15101510
return self._dtype
@@ -2092,7 +2092,7 @@ def _maybe_convert_to_int_keys(convert_dates: dict, varlist: list[Hashable]) ->
20922092
new_dict = {}
20932093
for key in convert_dates:
20942094
if not convert_dates[key].startswith("%"): # make sure proper fmts
2095-
convert_dates[key] = "%" + convert_dates[key]
2095+
convert_dates[key] = f"%{convert_dates[key]}"
20962096
if key in varlist:
20972097
new_dict.update({varlist.index(key): convert_dates[key]})
20982098
else:
@@ -2170,7 +2170,7 @@ def _dtype_to_default_stata_fmt(
21702170
return "%9s"
21712171
else:
21722172
raise ValueError(excessive_string_length_error.format(column.name))
2173-
return "%" + str(max(itemsize, 1)) + "s"
2173+
return f"%{max(itemsize, 1)}s"
21742174
elif dtype == np.float64:
21752175
return "%10.0g"
21762176
elif dtype == np.float32:
@@ -2476,19 +2476,19 @@ def _check_column_names(self, data: DataFrame) -> DataFrame:
24762476

24772477
# Variable name must not be a reserved word
24782478
if name in self.RESERVED_WORDS:
2479-
name = "_" + name
2479+
name = f"_{name}"
24802480

24812481
# Variable name may not start with a number
24822482
if "0" <= name[0] <= "9":
2483-
name = "_" + name
2483+
name = f"_{name}"
24842484

24852485
name = name[: min(len(name), 32)]
24862486

24872487
if not name == orig_name:
24882488
# check for duplicates
24892489
while columns.count(name) > 0:
24902490
# prepend ascending number to avoid duplicates
2491-
name = "_" + str(duplicate_var_id) + name
2491+
name = f"_{duplicate_var_id}{name}"
24922492
name = name[: min(len(name), 32)]
24932493
duplicate_var_id += 1
24942494
converted_names[orig_name] = name
@@ -3249,7 +3249,7 @@ def _tag(val: str | bytes, tag: str) -> bytes:
32493249
"""Surround val with <tag></tag>"""
32503250
if isinstance(val, str):
32513251
val = bytes(val, "utf-8")
3252-
return bytes("<" + tag + ">", "utf-8") + val + bytes("</" + tag + ">", "utf-8")
3252+
return bytes(f"<{tag}>", "utf-8") + val + bytes(f"</{tag}>", "utf-8")
32533253

32543254
def _update_map(self, tag: str) -> None:
32553255
"""Update map location for tag with file position"""

0 commit comments

Comments
 (0)