Skip to content

Commit 42adb9f

Browse files
author
vrserpa
authored
PERF: Use list comprehension to join strings (#41753) (#42173)
* PERF: Use list comprehension to join strings (#41753) * Fix pre-commit failures
1 parent 0f3e61c commit 42adb9f

File tree

35 files changed

+76
-71
lines changed

35 files changed

+76
-71
lines changed

asv_bench/benchmarks/io/csv.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,8 @@ class ReadCSVFloatPrecision(StringIORewind):
291291

292292
def setup(self, sep, decimal, float_precision):
293293
floats = [
294-
"".join(random.choice(string.digits) for _ in range(28)) for _ in range(15)
294+
"".join([random.choice(string.digits) for _ in range(28)])
295+
for _ in range(15)
295296
]
296297
rows = sep.join([f"0{decimal}" + "{}"] * 3) + "\n"
297298
data = rows * 5
@@ -395,7 +396,7 @@ class ReadCSVCachedParseDates(StringIORewind):
395396
param_names = ["do_cache", "engine"]
396397

397398
def setup(self, do_cache, engine):
398-
data = ("\n".join(f"10/{year}" for year in range(2000, 2100)) + "\n") * 10
399+
data = ("\n".join([f"10/{year}" for year in range(2000, 2100)]) + "\n") * 10
399400
self.StringIO_input = StringIO(data)
400401

401402
def time_read_csv_cached(self, do_cache, engine):

doc/source/user_guide/io.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5699,7 +5699,7 @@ Example of a callable using PostgreSQL `COPY clause
56995699
writer.writerows(data_iter)
57005700
s_buf.seek(0)
57015701

5702-
columns = ', '.join('"{}"'.format(k) for k in keys)
5702+
columns = ', '.join(['"{}"'.format(k) for k in keys])
57035703
if table.schema:
57045704
table_name = '{}.{}'.format(table.schema, table.name)
57055705
else:

pandas/_config/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _describe_option(pat: str = "", _print_desc: bool = True):
157157
if len(keys) == 0:
158158
raise OptionError("No such keys(s)")
159159

160-
s = "\n".join(_build_option_description(k) for k in keys)
160+
s = "\n".join([_build_option_description(k) for k in keys])
161161

162162
if _print_desc:
163163
print(s)

pandas/core/computation/engines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _check_ne_builtin_clash(expr: Expr) -> None:
3737
overlap = names & _ne_builtins
3838

3939
if overlap:
40-
s = ", ".join(repr(x) for x in overlap)
40+
s = ", ".join([repr(x) for x in overlap])
4141
raise NumExprClobberingError(
4242
f'Variables in expression "{expr}" overlap with builtins: ({s})'
4343
)

pandas/core/computation/parsing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def create_valid_python_identifier(name: str) -> str:
5757
}
5858
)
5959

60-
name = "".join(special_characters_replacements.get(char, char) for char in name)
60+
name = "".join([special_characters_replacements.get(char, char) for char in name])
6161
name = "BACKTICK_QUOTED_STRING_" + name
6262

6363
if not name.isidentifier():

pandas/core/computation/pytables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ def __init__(
575575
else:
576576
w = _validate_where(w)
577577
where[idx] = w
578-
_where = " & ".join(f"({w})" for w in com.flatten(where))
578+
_where = " & ".join([f"({w})" for w in com.flatten(where)])
579579
else:
580580
# _validate_where ensures we otherwise have a string
581581
_where = where

pandas/core/computation/scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _raw_hex_id(obj) -> str:
5050
"""Return the padded hexadecimal id of ``obj``."""
5151
# interpret as a pointer since that's what really what id returns
5252
packed = struct.pack("@P", id(obj))
53-
return "".join(_replacer(x) for x in packed)
53+
return "".join([_replacer(x) for x in packed])
5454

5555

5656
DEFAULT_GLOBALS = {

pandas/core/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10980,7 +10980,7 @@ def last_valid_index(self) -> Hashable | None:
1098010980
def _doc_params(cls):
1098110981
"""Return a tuple of the doc params."""
1098210982
axis_descr = (
10983-
f"{{{', '.join(f'{a} ({i})' for i, a in enumerate(cls._AXIS_ORDERS))}}}"
10983+
f"{{{', '.join([f'{a} ({i})' for i, a in enumerate(cls._AXIS_ORDERS)])}}}"
1098410984
)
1098510985
name = cls._constructor_sliced.__name__ if cls._AXIS_LEN > 1 else "scalar"
1098610986
name2 = cls.__name__

pandas/core/internals/blocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def __repr__(self) -> str:
281281
result = f"{name}: {len(self)} dtype: {self.dtype}"
282282
else:
283283

284-
shape = " x ".join(str(s) for s in self.shape)
284+
shape = " x ".join([str(s) for s in self.shape])
285285
result = f"{name}: {self.mgr_locs.indexer}, {shape}, dtype: {self.dtype}"
286286

287287
return result

pandas/io/formats/excel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def _generate_body(self, coloffset: int) -> Iterable[ExcelCell]:
769769
series = self.df.iloc[:, colidx]
770770
for i, val in enumerate(series):
771771
if styles is not None:
772-
css = ";".join(a + ":" + str(v) for (a, v) in styles[i, colidx])
772+
css = ";".join([a + ":" + str(v) for (a, v) in styles[i, colidx]])
773773
xlstyle = self.style_converter(css)
774774
yield ExcelCell(self.rowcounter + i, colidx + coloffset, val, xlstyle)
775775

0 commit comments

Comments
 (0)