Skip to content

Commit 2faf709

Browse files
authored
STY: Enable ruff perflint (#54236)
* some check implmented * Include rules * Enable perflint * type
1 parent b3b1beb commit 2faf709

File tree

20 files changed

+102
-111
lines changed

20 files changed

+102
-111
lines changed

doc/source/conf.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,8 @@
348348
methods = [
349349
x for x in dir(klass) if not x.startswith("_") or x in ("__iter__", "__array__")
350350
]
351-
352-
for method in methods:
353-
# ... and each of its public methods
354-
moved_api_pages.append((f"{old}.{method}", f"{new}.{method}"))
351+
# ... and each of its public methods
352+
moved_api_pages.extend((f"{old}.{method}", f"{new}.{method}") for method in methods)
355353

356354
if include_api:
357355
html_additional_pages = {

pandas/core/frame.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5815,21 +5815,21 @@ def set_index(
58155815
# GH 49473 Use "lazy copy" with Copy-on-Write
58165816
frame = self.copy(deep=None)
58175817

5818-
arrays = []
5818+
arrays: list[Index] = []
58195819
names: list[Hashable] = []
58205820
if append:
58215821
names = list(self.index.names)
58225822
if isinstance(self.index, MultiIndex):
5823-
for i in range(self.index.nlevels):
5824-
arrays.append(self.index._get_level_values(i))
5823+
arrays.extend(
5824+
self.index._get_level_values(i) for i in range(self.index.nlevels)
5825+
)
58255826
else:
58265827
arrays.append(self.index)
58275828

58285829
to_remove: list[Hashable] = []
58295830
for col in keys:
58305831
if isinstance(col, MultiIndex):
5831-
for n in range(col.nlevels):
5832-
arrays.append(col._get_level_values(n))
5832+
arrays.extend(col._get_level_values(n) for n in range(col.nlevels))
58335833
names.extend(col.names)
58345834
elif isinstance(col, (Index, Series)):
58355835
# if Index then not MultiIndex (treated above)

pandas/core/groupby/generic.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,7 @@ def _aggregate_multiple_funcs(self, arg, *args, **kwargs) -> DataFrame:
345345
arg = [(x, x) if not isinstance(x, (tuple, list)) else x for x in arg]
346346
else:
347347
# list of functions / function names
348-
columns = []
349-
for f in arg:
350-
columns.append(com.get_callable_name(f) or f)
351-
348+
columns = (com.get_callable_name(f) or f for f in arg)
352349
arg = zip(columns, arg)
353350

354351
results: dict[base.OutputKey, DataFrame | Series] = {}

pandas/core/internals/managers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,7 @@ def get_bool_data(self, copy: bool = False) -> Self:
458458

459459
elif blk.is_object:
460460
nbs = blk._split()
461-
for nb in nbs:
462-
if nb.is_bool:
463-
new_blocks.append(nb)
461+
new_blocks.extend(nb for nb in nbs if nb.is_bool)
464462

465463
return self._combine(new_blocks, copy)
466464

pandas/core/methods/describe.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,12 @@ def _select_data(self) -> DataFrame:
202202
def reorder_columns(ldesc: Sequence[Series]) -> list[Hashable]:
203203
"""Set a convenient order for rows for display."""
204204
names: list[Hashable] = []
205+
seen_names: set[Hashable] = set()
205206
ldesc_indexes = sorted((x.index for x in ldesc), key=len)
206207
for idxnames in ldesc_indexes:
207208
for name in idxnames:
208-
if name not in names:
209+
if name not in seen_names:
210+
seen_names.add(name)
209211
names.append(name)
210212
return names
211213

pandas/core/reshape/reshape.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,7 @@ def _unstack_multiple(
467467
new_names = [data.columns.name] + cnames
468468

469469
new_codes = [unstcols.codes[0]]
470-
for rec in recons_codes:
471-
new_codes.append(rec.take(unstcols.codes[-1]))
470+
new_codes.extend(rec.take(unstcols.codes[-1]) for rec in recons_codes)
472471

473472
new_columns = MultiIndex(
474473
levels=new_levels, codes=new_codes, names=new_names, verify_integrity=False

pandas/io/excel/_odfreader.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ def get_sheet_data(
154154
# add blank rows to our table
155155
table.extend([[self.empty_value]] * empty_rows)
156156
empty_rows = 0
157-
for _ in range(row_repeat):
158-
table.append(table_row)
157+
table.extend(table_row for _ in range(row_repeat))
159158
if file_rows_needed is not None and len(table) >= file_rows_needed:
160159
break
161160

pandas/io/formats/printing.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ def adjoin(space: int, *lists: list[str], **kwargs) -> str:
4444
strlen = kwargs.pop("strlen", len)
4545
justfunc = kwargs.pop("justfunc", justify)
4646

47-
out_lines = []
4847
newLists = []
4948
lengths = [max(map(strlen, x)) + space for x in lists[:-1]]
5049
# not the last one
@@ -55,9 +54,7 @@ def adjoin(space: int, *lists: list[str], **kwargs) -> str:
5554
nl = ([" " * lengths[i]] * (maxLen - len(lst))) + nl
5655
newLists.append(nl)
5756
toJoin = zip(*newLists)
58-
for lines in toJoin:
59-
out_lines.append("".join(lines))
60-
return "\n".join(out_lines)
57+
return "\n".join("".join(lines) for lines in toJoin)
6158

6259

6360
def justify(texts: Iterable[str], max_len: int, mode: str = "right") -> list[str]:

pandas/io/parsers/python_parser.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,16 @@ def _remove_empty_lines(self, lines: list[list[Scalar]]) -> list[list[Scalar]]:
865865
filtered_lines : list of list of Scalars
866866
The same array of lines with the "empty" ones removed.
867867
"""
868-
ret = []
869-
for line in lines:
870-
# Remove empty lines and lines with only one whitespace value
868+
# Remove empty lines and lines with only one whitespace value
869+
ret = [
870+
line
871+
for line in lines
871872
if (
872873
len(line) > 1
873874
or len(line) == 1
874875
and (not isinstance(line[0], str) or line[0].strip())
875-
):
876-
ret.append(line)
876+
)
877+
]
877878
return ret
878879

879880
def _check_thousands(self, lines: list[list[Scalar]]) -> list[list[Scalar]]:

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,8 +1313,7 @@ def test_groupby_combined_aggs_cat_cols(grp_col_dict, exp_data):
13131313
multi_index_list = []
13141314
for k, v in grp_col_dict.items():
13151315
if isinstance(v, list):
1316-
for value in v:
1317-
multi_index_list.append([k, value])
1316+
multi_index_list.extend([k, value] for value in v)
13181317
else:
13191318
multi_index_list.append([k, v])
13201319
multi_index = MultiIndex.from_tuples(tuple(multi_index_list))

0 commit comments

Comments
 (0)