Skip to content

Commit 9c799e2

Browse files
mroeschkejreback
authored andcommitted
CLN: Use generators instead of lists in built-in Python functions (#18276)
1 parent fa4ebd4 commit 9c799e2

File tree

20 files changed

+73
-74
lines changed

20 files changed

+73
-74
lines changed

pandas/_libs/lib.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True):
14841484
if len(slices) == 1:
14851485
yield blkno, slice(slices[0][0], slices[0][1])
14861486
else:
1487-
tot_len = sum([stop - start for start, stop in slices])
1487+
tot_len = sum(stop - start for start, stop in slices)
14881488
result = np.empty(tot_len, dtype=np.int64)
14891489
res_view = result
14901490

pandas/compat/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
266266
Calculate display width considering unicode East Asian Width
267267
"""
268268
if isinstance(data, text_type):
269-
return sum([_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data])
269+
return sum(_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data)
270270
else:
271271
return len(data)
272272

@@ -318,7 +318,7 @@ def east_asian_len(data, encoding=None, ambiguous_width=1):
318318
data = data.decode(encoding)
319319
except UnicodeError:
320320
pass
321-
return sum([_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data])
321+
return sum(_EAW_MAP.get(east_asian_width(c), ambiguous_width) for c in data)
322322
else:
323323
return len(data)
324324

pandas/core/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,13 @@ def _agg(arg, func):
486486

487487
def is_any_series():
488488
# return a boolean if we have *any* nested series
489-
return any([isinstance(r, ABCSeries)
490-
for r in compat.itervalues(result)])
489+
return any(isinstance(r, ABCSeries)
490+
for r in compat.itervalues(result))
491491

492492
def is_any_frame():
493493
# return a boolean if we have *any* nested series
494-
return any([isinstance(r, ABCDataFrame)
495-
for r in compat.itervalues(result)])
494+
return any(isinstance(r, ABCDataFrame)
495+
for r in compat.itervalues(result))
496496

497497
if isinstance(result, list):
498498
return concat(result, keys=keys, axis=1), True

pandas/core/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ def inner(x):
800800
from pandas.io.formats.printing import pprint_thing as pp
801801
if x not in legal_values:
802802

803-
if not any([c(x) for c in callables]):
803+
if not any(c(x) for c in callables):
804804
pp_values = pp("|".join(lmap(pp, legal_values)))
805805
msg = "Value must be one of {pp_values}"
806806
if len(callables):

pandas/core/frame.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def _repr_fits_horizontal_(self, ignore_width=False):
601601

602602
d.to_string(buf=buf)
603603
value = buf.getvalue()
604-
repr_width = max([len(l) for l in value.split('\n')])
604+
repr_width = max(len(l) for l in value.split('\n'))
605605

606606
return repr_width < width
607607

@@ -1798,7 +1798,7 @@ def info(self, verbose=None, buf=None, max_cols=None, memory_usage=None,
17981798
def _verbose_repr():
17991799
lines.append('Data columns (total %d columns):' %
18001800
len(self.columns))
1801-
space = max([len(pprint_thing(k)) for k in self.columns]) + 4
1801+
space = max(len(pprint_thing(k)) for k in self.columns) + 4
18021802
counts = None
18031803

18041804
tmpl = "%s%s"
@@ -6424,7 +6424,7 @@ def convert(arr):
64246424

64256425

64266426
def _get_names_from_index(data):
6427-
has_some_name = any([getattr(s, 'name', None) is not None for s in data])
6427+
has_some_name = any(getattr(s, 'name', None) is not None for s in data)
64286428
if not has_some_name:
64296429
return _default_index(len(data))
64306430

pandas/core/generic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,8 +1006,8 @@ def _set_axis_name(self, name, axis=0, inplace=False):
10061006
# Comparisons
10071007

10081008
def _indexed_same(self, other):
1009-
return all([self._get_axis(a).equals(other._get_axis(a))
1010-
for a in self._AXIS_ORDERS])
1009+
return all(self._get_axis(a).equals(other._get_axis(a))
1010+
for a in self._AXIS_ORDERS)
10111011

10121012
def __neg__(self):
10131013
values = _values_from_object(self)
@@ -2989,8 +2989,8 @@ def reindex(self, *args, **kwargs):
29892989

29902990
# if all axes that are requested to reindex are equal, then only copy
29912991
# if indicated must have index names equal here as well as values
2992-
if all([self._get_axis(axis).identical(ax)
2993-
for axis, ax in axes.items() if ax is not None]):
2992+
if all(self._get_axis(axis).identical(ax)
2993+
for axis, ax in axes.items() if ax is not None):
29942994
if copy:
29952995
return self.copy()
29962996
return self
@@ -5886,8 +5886,8 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
58865886

58875887
# if we are NOT aligned, raise as we cannot where index
58885888
if (axis is None and
5889-
not all([other._get_axis(i).equals(ax)
5890-
for i, ax in enumerate(self.axes)])):
5889+
not all(other._get_axis(i).equals(ax)
5890+
for i, ax in enumerate(self.axes))):
58915891
raise InvalidIndexError
58925892

58935893
# slice me out of the other

pandas/core/groupby.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,8 +3890,7 @@ def first_not_none(values):
38903890
# values are not series or array-like but scalars
38913891
else:
38923892
# only coerce dates if we find at least 1 datetime
3893-
coerce = True if any([isinstance(x, Timestamp)
3894-
for x in values]) else False
3893+
coerce = any(isinstance(x, Timestamp) for x in values)
38953894
# self._selection_name not passed through to Series as the
38963895
# result should not take the name of original selection
38973896
# of columns
@@ -4303,8 +4302,8 @@ def _reindex_output(self, result):
43034302
return result
43044303
elif len(groupings) == 1:
43054304
return result
4306-
elif not any([isinstance(ping.grouper, (Categorical, CategoricalIndex))
4307-
for ping in groupings]):
4305+
elif not any(isinstance(ping.grouper, (Categorical, CategoricalIndex))
4306+
for ping in groupings):
43084307
return result
43094308

43104309
levels_list = [ping.group_index for ping in groupings]

pandas/core/indexes/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ def _extend_line(s, line, value, display_width, next_line_prefix):
907907

908908
def best_len(values):
909909
if values:
910-
return max([adj.len(x) for x in values])
910+
return max(adj.len(x) for x in values)
911911
else:
912912
return 0
913913

@@ -4246,7 +4246,7 @@ def _trim_front(strings):
42464246
Trims zeros and decimal points
42474247
"""
42484248
trimmed = strings
4249-
while len(strings) > 0 and all([x[0] == ' ' for x in trimmed]):
4249+
while len(strings) > 0 and all(x[0] == ' ' for x in trimmed):
42504250
trimmed = [x[1:] for x in trimmed]
42514251
return trimmed
42524252

pandas/core/indexes/multi.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def _is_memory_usage_qualified(self):
461461
""" return a boolean if we need a qualified .info display """
462462
def f(l):
463463
return 'mixed' in l or 'string' in l or 'unicode' in l
464-
return any([f(l) for l in self._inferred_type_levels])
464+
return any(f(l) for l in self._inferred_type_levels)
465465

466466
@Appender(Index.memory_usage.__doc__)
467467
def memory_usage(self, deep=False):
@@ -489,9 +489,9 @@ def _nbytes(self, deep=False):
489489
# for implementations with no useful getsizeof (PyPy)
490490
objsize = 24
491491

492-
level_nbytes = sum((i.memory_usage(deep=deep) for i in self.levels))
493-
label_nbytes = sum((i.nbytes for i in self.labels))
494-
names_nbytes = sum((getsizeof(i, objsize) for i in self.names))
492+
level_nbytes = sum(i.memory_usage(deep=deep) for i in self.levels)
493+
label_nbytes = sum(i.nbytes for i in self.labels)
494+
names_nbytes = sum(getsizeof(i, objsize) for i in self.names)
495495
result = level_nbytes + label_nbytes + names_nbytes
496496

497497
# include our engine hashtable
@@ -2214,12 +2214,12 @@ def partial_selection(key, indexer=None):
22142214
# here we have a completely specified key, but are
22152215
# using some partial string matching here
22162216
# GH4758
2217-
all_dates = [(l.is_all_dates and
2217+
all_dates = ((l.is_all_dates and
22182218
not isinstance(k, compat.string_types))
2219-
for k, l in zip(key, self.levels)]
2219+
for k, l in zip(key, self.levels))
22202220
can_index_exactly = any(all_dates)
2221-
if (any([l.is_all_dates
2222-
for k, l in zip(key, self.levels)]) and
2221+
if (any(l.is_all_dates
2222+
for k, l in zip(key, self.levels)) and
22232223
not can_index_exactly):
22242224
indexer = self.get_loc(key)
22252225

pandas/core/indexes/range.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ def nbytes(self):
193193
On implementations where this is undetermined (PyPy)
194194
assume 24 bytes for each value
195195
"""
196-
return sum([getsizeof(getattr(self, v), 24) for v in
197-
['_start', '_stop', '_step']])
196+
return sum(getsizeof(getattr(self, v), 24) for v in
197+
['_start', '_stop', '_step'])
198198

199199
def memory_usage(self, deep=False):
200200
"""
@@ -613,8 +613,8 @@ def _evaluate_numeric_binop(self, other):
613613
# for compat with numpy / Int64Index
614614
# even if we can represent as a RangeIndex, return
615615
# as a Float64Index if we have float-like descriptors
616-
if not all([is_integer(x) for x in
617-
[rstart, rstop, rstep]]):
616+
if not all(is_integer(x) for x in
617+
[rstart, rstop, rstep]):
618618
result = result.astype('float64')
619619

620620
return result

0 commit comments

Comments
 (0)