Skip to content

Commit b37e6f4

Browse files
rthPingviinituutti
authored andcommitted
MAINT Use list and dict comprehension (pandas-dev#23894)
1 parent 6a1ddb1 commit b37e6f4

27 files changed

+73
-126
lines changed

pandas/core/computation/align.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ def _align_core_single_unary_op(term):
3030

3131

3232
def _zip_axes_from_type(typ, new_axes):
33-
axes = {}
34-
for ax_ind, ax_name in compat.iteritems(typ._AXIS_NAMES):
35-
axes[ax_name] = new_axes[ax_ind]
33+
axes = {ax_name: new_axes[ax_ind]
34+
for ax_ind, ax_name in compat.iteritems(typ._AXIS_NAMES)}
3635
return axes
3736

3837

pandas/core/config.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,8 @@ def __init__(self, *args):
395395
self.ops = list(zip(args[::2], args[1::2]))
396396

397397
def __enter__(self):
398-
undo = []
399-
for pat, val in self.ops:
400-
undo.append((pat, _get_option(pat, silent=True)))
401-
402-
self.undo = undo
398+
self.undo = [(pat, _get_option(pat, silent=True))
399+
for pat, val in self.ops]
403400

404401
for pat, val in self.ops:
405402
_set_option(pat, val, silent=True)

pandas/core/dtypes/concat.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -373,10 +373,8 @@ def _maybe_unwrap(x):
373373
if sort_categories:
374374
categories = categories.sort_values()
375375

376-
new_codes = []
377-
for c in to_union:
378-
new_codes.append(_recode_for_categories(c.codes, c.categories,
379-
categories))
376+
new_codes = [_recode_for_categories(c.codes, c.categories, categories)
377+
for c in to_union]
380378
new_codes = np.concatenate(new_codes)
381379
else:
382380
# ordered - to show a proper error message

pandas/core/frame.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -3569,11 +3569,8 @@ def reindexer(value):
35693569

35703570
@property
35713571
def _series(self):
3572-
result = {}
3573-
for idx, item in enumerate(self.columns):
3574-
result[item] = Series(self._data.iget(idx), index=self.index,
3575-
name=item)
3576-
return result
3572+
return {item: Series(self._data.iget(idx), index=self.index, name=item)
3573+
for idx, item in enumerate(self.columns)}
35773574

35783575
def lookup(self, row_labels, col_labels):
35793576
"""
@@ -3593,9 +3590,8 @@ def lookup(self, row_labels, col_labels):
35933590
-----
35943591
Akin to::
35953592
3596-
result = []
3597-
for row, col in zip(row_labels, col_labels):
3598-
result.append(df.get_value(row, col))
3593+
result = [df.get_value(row, col)
3594+
for row, col in zip(row_labels, col_labels)]
35993595
36003596
Examples
36013597
--------
@@ -4600,10 +4596,8 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False,
46004596
if len(by) > 1:
46014597
from pandas.core.sorting import lexsort_indexer
46024598

4603-
keys = []
4604-
for x in by:
4605-
k = self._get_label_or_level_values(x, axis=axis)
4606-
keys.append(k)
4599+
keys = [self._get_label_or_level_values(x, axis=axis)
4600+
for x in by]
46074601
indexer = lexsort_indexer(keys, orders=ascending,
46084602
na_position=na_position)
46094603
indexer = ensure_platform_int(indexer)

pandas/core/groupby/ops.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,8 @@ def groups(self):
678678

679679
# this is mainly for compat
680680
# GH 3881
681-
result = {}
682-
for key, value in zip(self.binlabels, self.bins):
683-
if key is not NaT:
684-
result[key] = value
681+
result = {key: value for key, value in zip(self.binlabels, self.bins)
682+
if key is not NaT}
685683
return result
686684

687685
@property

pandas/core/internals/blocks.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -2295,10 +2295,7 @@ def convert(self, *args, **kwargs):
22952295
'convert_timedeltas']
22962296
fn_inputs += ['copy']
22972297

2298-
fn_kwargs = {}
2299-
for key in fn_inputs:
2300-
if key in kwargs:
2301-
fn_kwargs[key] = kwargs[key]
2298+
fn_kwargs = {key: kwargs[key] for key in fn_inputs if key in kwargs}
23022299

23032300
# operate column-by-column
23042301
def f(m, v, i):

pandas/core/panel.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,8 @@ def _compare_constructor(self, other, func):
336336
raise Exception('Can only compare identically-labeled '
337337
'same type objects')
338338

339-
new_data = {}
340-
for col in self._info_axis:
341-
new_data[col] = func(self[col], other[col])
339+
new_data = {col: func(self[col], other[col])
340+
for col in self._info_axis}
342341

343342
d = self._construct_axes_dict(copy=False)
344343
return self._constructor(data=new_data, **d)
@@ -949,9 +948,8 @@ def to_frame(self, filter_observations=True):
949948
# size = N * K
950949
selector = slice(None, None)
951950

952-
data = {}
953-
for item in self.items:
954-
data[item] = self[item].values.ravel()[selector]
951+
data = {item: self[item].values.ravel()[selector]
952+
for item in self.items}
955953

956954
def construct_multi_parts(idx, n_repeat, n_shuffle=1):
957955
# Replicates and shuffles MultiIndex, returns individual attributes

pandas/core/reshape/melt.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,8 @@ def melt_stub(df, stub, i, j, value_vars, sep):
448448
value_vars_flattened = [e for sublist in value_vars for e in sublist]
449449
id_vars = list(set(df.columns.tolist()).difference(value_vars_flattened))
450450

451-
melted = []
452-
for s, v in zip(stubnames, value_vars):
453-
melted.append(melt_stub(df, s, i, j, v, sep))
451+
melted = [melt_stub(df, s, i, j, v, sep)
452+
for s, v in zip(stubnames, value_vars)]
454453
melted = melted[0].join(melted[1:], how='outer')
455454

456455
if len(i) == 1:

pandas/core/sparse/frame.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,8 @@ def to_dense(self):
339339
def _apply_columns(self, func):
340340
""" get new SparseDataFrame applying func to each columns """
341341

342-
new_data = {}
343-
for col, series in compat.iteritems(self):
344-
new_data[col] = func(series)
342+
new_data = {col: func(series)
343+
for col, series in compat.iteritems(self)}
345344

346345
return self._constructor(
347346
data=new_data, index=self.index, columns=self.columns,

pandas/core/window.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -2479,9 +2479,8 @@ def dataframe_from_int_dict(data, frame_template):
24792479
else:
24802480
raise ValueError("'pairwise' is not True/False")
24812481
else:
2482-
results = {}
2483-
for i, col in enumerate(arg1.columns):
2484-
results[i] = f(*_prep_binary(arg1.iloc[:, i], arg2))
2482+
results = {i: f(*_prep_binary(arg1.iloc[:, i], arg2))
2483+
for i, col in enumerate(arg1.columns)}
24852484
return dataframe_from_int_dict(results, arg1)
24862485

24872486
else:

pandas/io/excel.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,9 @@ def _parse_cell(cell_contents, cell_typ):
586586
usecols = _maybe_convert_usecols(usecols)
587587

588588
for i in range(sheet.nrows):
589-
row = []
590-
for j, (value, typ) in enumerate(zip(sheet.row_values(i),
591-
sheet.row_types(i))):
592-
row.append(_parse_cell(value, typ))
589+
row = [_parse_cell(value, typ)
590+
for value, typ in zip(sheet.row_values(i),
591+
sheet.row_types(i))]
593592
data.append(row)
594593

595594
if sheet.nrows == 0:

pandas/io/formats/printing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ def _pprint_seq(seq, _nest_lvl=0, max_seq_items=None, **kwds):
110110
nitems = max_seq_items or get_option("max_seq_items") or len(seq)
111111

112112
s = iter(seq)
113-
r = []
114-
for i in range(min(nitems, len(seq))): # handle sets, no slicing
115-
r.append(pprint_thing(
116-
next(s), _nest_lvl + 1, max_seq_items=max_seq_items, **kwds))
113+
# handle sets, no slicing
114+
r = [pprint_thing(next(s),
115+
_nest_lvl + 1, max_seq_items=max_seq_items, **kwds)
116+
for i in range(min(nitems, len(seq)))]
117117
body = ", ".join(r)
118118

119119
if nitems < len(seq):

pandas/io/formats/style.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1349,10 +1349,8 @@ def _get_level_lengths(index, hidden_elements=None):
13491349
elif(j not in hidden_elements):
13501350
lengths[(i, last_label)] += 1
13511351

1352-
non_zero_lengths = {}
1353-
for element, length in lengths.items():
1354-
if(length >= 1):
1355-
non_zero_lengths[element] = length
1352+
non_zero_lengths = {
1353+
element: length for element, length in lengths.items() if length >= 1}
13561354

13571355
return non_zero_lengths
13581356

pandas/io/sas/sas_xport.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,8 @@ def _read_header(self):
353353
self.columns = [x['name'].decode() for x in self.fields]
354354

355355
# Setup the dtype.
356-
dtypel = []
357-
for i, field in enumerate(self.fields):
358-
dtypel.append(('s' + str(i), "S" + str(field['field_length'])))
356+
dtypel = [('s' + str(i), "S" + str(field['field_length']))
357+
for i, field in enumerate(self.fields)]
359358
dtype = np.dtype(dtypel)
360359
self._dtype = dtype
361360

pandas/io/stata.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2947,10 +2947,10 @@ def _update_strl_names(self):
29472947
def _convert_strls(self, data):
29482948
"""Convert columns to StrLs if either very large or in the
29492949
convert_strl variable"""
2950-
convert_cols = []
2951-
for i, col in enumerate(data):
2952-
if self.typlist[i] == 32768 or col in self._convert_strl:
2953-
convert_cols.append(col)
2950+
convert_cols = [
2951+
col for i, col in enumerate(data)
2952+
if self.typlist[i] == 32768 or col in self._convert_strl]
2953+
29542954
if convert_cols:
29552955
ssw = StataStrLWriter(data, convert_cols)
29562956
tab, new_data = ssw.generate_table()

pandas/plotting/_core.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,8 @@ def __init__(self, data, kind=None, by=None, subplots=False, sharex=None,
157157
# parse errorbar input if given
158158
xerr = kwds.pop('xerr', None)
159159
yerr = kwds.pop('yerr', None)
160-
self.errors = {}
161-
for kw, err in zip(['xerr', 'yerr'], [xerr, yerr]):
162-
self.errors[kw] = self._parse_errorbars(kw, err)
160+
self.errors = {kw: self._parse_errorbars(kw, err)
161+
for kw, err in zip(['xerr', 'yerr'], [xerr, yerr])}
163162

164163
if not isinstance(secondary_y, (bool, tuple, list,
165164
np.ndarray, ABCIndexClass)):
@@ -1721,9 +1720,7 @@ def result(self):
17211720
_klasses = [LinePlot, BarPlot, BarhPlot, KdePlot, HistPlot, BoxPlot,
17221721
ScatterPlot, HexBinPlot, AreaPlot, PiePlot]
17231722

1724-
_plot_klass = {}
1725-
for klass in _klasses:
1726-
_plot_klass[klass._kind] = klass
1723+
_plot_klass = {klass._kind: klass for klass in _klasses}
17271724

17281725

17291726
def _plot(data, x=None, y=None, subplots=False,

pandas/tests/frame/test_axis_select_reindex.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,7 @@ def test_align(self):
634634

635635
left, right = self.frame.align(s, broadcast_axis=1)
636636
tm.assert_index_equal(left.index, self.frame.index)
637-
expected = {}
638-
for c in self.frame.columns:
639-
expected[c] = s
637+
expected = {c: s for c in self.frame.columns}
640638
expected = DataFrame(expected, index=self.frame.index,
641639
columns=self.frame.columns)
642640
tm.assert_frame_equal(right, expected)

pandas/tests/frame/test_indexing.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1782,11 +1782,9 @@ def test_get_value(self):
17821782

17831783
def test_lookup(self):
17841784
def alt(df, rows, cols, dtype):
1785-
result = []
1786-
for r, c in zip(rows, cols):
1787-
with tm.assert_produces_warning(FutureWarning,
1788-
check_stacklevel=False):
1789-
result.append(df.get_value(r, c))
1785+
with tm.assert_produces_warning(FutureWarning,
1786+
check_stacklevel=False):
1787+
result = [df.get_value(r, c) for r, c in zip(rows, cols)]
17901788
return np.array(result, dtype=dtype)
17911789

17921790
def testit(df):

pandas/tests/frame/test_replace.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -806,9 +806,8 @@ def test_replace_input_formats_listlike(self):
806806
df = DataFrame({'A': [np.nan, 0, np.inf], 'B': [0, 2, 5],
807807
'C': ['', 'asdf', 'fd']})
808808
filled = df.replace(to_rep, values)
809-
expected = {}
810-
for k, v in compat.iteritems(df):
811-
expected[k] = v.replace(to_rep[k], values[k])
809+
expected = {k: v.replace(to_rep[k], values[k])
810+
for k, v in compat.iteritems(df)}
812811
assert_frame_equal(filled, DataFrame(expected))
813812

814813
result = df.replace([0, 2, 5], [5, 2, 0])
@@ -821,9 +820,8 @@ def test_replace_input_formats_listlike(self):
821820
df = DataFrame({'A': [np.nan, 0, np.nan], 'B': [0, 2, 5],
822821
'C': ['', 'asdf', 'fd']})
823822
filled = df.replace(np.nan, values)
824-
expected = {}
825-
for k, v in compat.iteritems(df):
826-
expected[k] = v.replace(np.nan, values[k])
823+
expected = {k: v.replace(np.nan, values[k])
824+
for k, v in compat.iteritems(df)}
827825
assert_frame_equal(filled, DataFrame(expected))
828826

829827
# list to list
@@ -844,9 +842,8 @@ def test_replace_input_formats_scalar(self):
844842
# dict to scalar
845843
to_rep = {'A': np.nan, 'B': 0, 'C': ''}
846844
filled = df.replace(to_rep, 0)
847-
expected = {}
848-
for k, v in compat.iteritems(df):
849-
expected[k] = v.replace(to_rep[k], 0)
845+
expected = {k: v.replace(to_rep[k], 0)
846+
for k, v in compat.iteritems(df)}
850847
assert_frame_equal(filled, DataFrame(expected))
851848

852849
pytest.raises(TypeError, df.replace, to_rep, [np.nan, 0, ''])

pandas/tests/generic/test_generic.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,8 @@ def test_rename(self):
9292
def test_get_numeric_data(self):
9393

9494
n = 4
95-
kwargs = {}
96-
for i in range(self._ndim):
97-
kwargs[self._typ._AXIS_NAMES[i]] = list(range(n))
95+
kwargs = {self._typ._AXIS_NAMES[i]: list(range(n))
96+
for i in range(self._ndim)}
9897

9998
# get the numeric data
10099
o = self._construct(n, **kwargs)

pandas/tests/groupby/aggregate/test_cython.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def test_cythonized_aggers(op_name):
4545

4646
# single column
4747
grouped = df.drop(['B'], axis=1).groupby('A')
48-
exp = {}
49-
for cat, group in grouped:
50-
exp[cat] = op(group['C'])
48+
exp = {cat: op(group['C']) for cat, group in grouped}
5149
exp = DataFrame({'C': exp})
5250
exp.index.name = 'A'
5351
result = op(grouped)

pandas/tests/groupby/test_groupby.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,7 @@ def test_attr_wrapper(ts):
348348

349349
# this is pretty cool
350350
result = grouped.describe()
351-
expected = {}
352-
for name, gp in grouped:
353-
expected[name] = gp.describe()
351+
expected = {name: gp.describe() for name, gp in grouped}
354352
expected = DataFrame(expected).T
355353
assert_frame_equal(result, expected)
356354

@@ -1312,19 +1310,15 @@ def test_skip_group_keys():
13121310
grouped = tsf.groupby(lambda x: x.month, group_keys=False)
13131311
result = grouped.apply(lambda x: x.sort_values(by='A')[:3])
13141312

1315-
pieces = []
1316-
for key, group in grouped:
1317-
pieces.append(group.sort_values(by='A')[:3])
1313+
pieces = [group.sort_values(by='A')[:3] for key, group in grouped]
13181314

13191315
expected = pd.concat(pieces)
13201316
assert_frame_equal(result, expected)
13211317

13221318
grouped = tsf['A'].groupby(lambda x: x.month, group_keys=False)
13231319
result = grouped.apply(lambda x: x.sort_values()[:3])
13241320

1325-
pieces = []
1326-
for key, group in grouped:
1327-
pieces.append(group.sort_values()[:3])
1321+
pieces = [group.sort_values()[:3] for key, group in grouped]
13281322

13291323
expected = pd.concat(pieces)
13301324
assert_series_equal(result, expected)

pandas/tests/groupby/test_grouping.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -727,9 +727,7 @@ def test_multi_iter_frame(self, three_group):
727727
df['k1'] = np.array(['b', 'b', 'b', 'a', 'a', 'a'])
728728
df['k2'] = np.array(['1', '1', '1', '2', '2', '2'])
729729
grouped = df.groupby(['k1', 'k2'])
730-
groups = {}
731-
for key, gp in grouped:
732-
groups[key] = gp
730+
groups = {key: gp for key, gp in grouped}
733731
assert len(groups) == 2
734732

735733
# axis = 1

0 commit comments

Comments
 (0)