Skip to content

Commit 4fce784

Browse files
mroeschkejreback
authored andcommitted
CLN: Replace comprehensions list/set/dict functions with corresponding symbols (#18383)
1 parent aec3347 commit 4fce784

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+160
-172
lines changed

asv_bench/benchmarks/frame_ctor.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def setup(self):
2323
self.some_dict = list(self.data.values())[0]
2424
self.dict_list = [dict(zip(self.columns, row)) for row in self.frame.values]
2525

26-
self.data2 = dict(
27-
((i, dict(((j, float(j)) for j in range(100)))) for i in
28-
range(2000)))
26+
self.data2 = {i: {j: float(j) for j in range(100)}
27+
for i in range(2000)}
28+
2929

3030
def time_frame_ctor_list_of_dict(self):
3131
DataFrame(self.dict_list)

asv_bench/benchmarks/packers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def _setup(self):
1818
self.N = 100000
1919
self.C = 5
2020
self.index = date_range('20000101', periods=self.N, freq='H')
21-
self.df = DataFrame(dict(('float{0}'.format(i), randn(self.N)) for i in range(self.C)), index=self.index)
21+
self.df = DataFrame({'float{0}'.format(i): randn(self.N) for i in range(self.C)}, index=self.index)
2222
self.df2 = self.df.copy()
2323
self.df2['object'] = [('%08x' % randrange((16 ** 8))) for _ in range(self.N)]
2424
self.remove(self.f)

asv_bench/benchmarks/replace.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class replace_large_dict(object):
2323
def setup(self):
2424
self.n = (10 ** 6)
2525
self.start_value = (10 ** 5)
26-
self.to_rep = dict(((i, (self.start_value + i)) for i in range(self.n)))
26+
self.to_rep = {i: self.start_value + i for i in range(self.n)}
2727
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
2828

2929
def time_replace_large_dict(self):
@@ -35,8 +35,8 @@ class replace_convert(object):
3535

3636
def setup(self):
3737
self.n = (10 ** 3)
38-
self.to_ts = dict(((i, pd.Timestamp(i)) for i in range(self.n)))
39-
self.to_td = dict(((i, pd.Timedelta(i)) for i in range(self.n)))
38+
self.to_ts = {i: pd.Timestamp(i) for i in range(self.n)}
39+
self.to_td = {i: pd.Timedelta(i) for i in range(self.n)}
4040
self.s = Series(np.random.randint(self.n, size=(10 ** 3)))
4141
self.df = DataFrame({'A': np.random.randint(self.n, size=(10 ** 3)),
4242
'B': np.random.randint(self.n, size=(10 ** 3))})

doc/sphinxext/numpydoc/phantom_import.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ def import_phantom_module(xml_file):
6060
# Sort items so that
6161
# - Base classes come before classes inherited from them
6262
# - Modules come before their contents
63-
all_nodes = dict((n.attrib['id'], n) for n in root)
64-
63+
all_nodes = {n.attrib['id']: n for n in root}
64+
6565
def _get_bases(node, recurse=False):
6666
bases = [x.attrib['ref'] for x in node.findall('base')]
6767
if recurse:

pandas/_libs/tslibs/offsets.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ from np_datetime cimport (pandas_datetimestruct,
3333
_MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
3434
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
3535
_int_to_month = {(k + 1): v for k, v in enumerate(_MONTHS)}
36-
_month_to_int = dict((v, k) for k, v in _int_to_month.items())
36+
_month_to_int = {v: k for k, v in _int_to_month.items()}
3737

3838

3939
class WeekDay(object):

pandas/_libs/tslibs/resolution.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ _ONE_HOUR = 60 * _ONE_MINUTE
5353
_ONE_DAY = 24 * _ONE_HOUR
5454

5555
DAYS = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
56-
_weekday_rule_aliases = dict((k, v) for k, v in enumerate(DAYS))
56+
_weekday_rule_aliases = {k: v for k, v in enumerate(DAYS)}
5757

5858
_MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL',
5959
'AUG', 'SEP', 'OCT', 'NOV', 'DEC']

pandas/_version.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
141141
if verbose:
142142
print("keywords are unexpanded, not using")
143143
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
144-
refs = set(r.strip() for r in refnames.strip("()").split(","))
144+
refs = {r.strip() for r in refnames.strip("()").split(",")}
145145
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
146146
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
147147
TAG = "tag: "
148-
tags = set(r[len(TAG):] for r in refs if r.startswith(TAG))
148+
tags = {r[len(TAG):] for r in refs if r.startswith(TAG)}
149149
if not tags:
150150
# Either we're using git < 1.8.3, or there really are no tags. We use
151151
# a heuristic: assume all version tags have a digit. The old git %d
@@ -154,7 +154,7 @@ def git_versions_from_keywords(keywords, tag_prefix, verbose):
154154
# between branches and tags. By ignoring refnames without digits, we
155155
# filter out many common branch names like "release" and
156156
# "stabilization", as well as "HEAD" and "master".
157-
tags = set(r for r in refs if re.search(r'\d', r))
157+
tags = {r for r in refs if re.search(r'\d', r)}
158158
if verbose:
159159
print("discarding '{}', no digits".format(",".join(refs - tags)))
160160
if verbose:

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def map_indices_py(arr):
347347
Returns a dictionary with (element, index) pairs for each element in the
348348
given array/list
349349
"""
350-
return dict((x, i) for i, x in enumerate(arr))
350+
return {x: i for i, x in enumerate(arr)}
351351

352352

353353
def union(*seqs):

pandas/core/dtypes/concat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def _concat_datetimetz(to_concat, name=None):
459459
it is used in DatetimeIndex.append also
460460
"""
461461
# do not pass tz to set because tzlocal cannot be hashed
462-
if len(set(str(x.dtype) for x in to_concat)) != 1:
462+
if len({str(x.dtype) for x in to_concat}) != 1:
463463
raise ValueError('to_concat must have the same tz')
464464
tz = to_concat[0].tz
465465
# no need to localize because internal repr will not be changed
@@ -525,7 +525,7 @@ def convert_sparse(x, axis):
525525
if len(typs) == 1:
526526
# concat input as it is if all inputs are sparse
527527
# and have the same fill_value
528-
fill_values = set(c.fill_value for c in to_concat)
528+
fill_values = {c.fill_value for c in to_concat}
529529
if len(fill_values) == 1:
530530
sp_values = [c.sp_values for c in to_concat]
531531
indexes = [c.sp_index.to_int_index() for c in to_concat]

pandas/core/frame.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
347347
elif isinstance(data, (np.ndarray, Series, Index)):
348348
if data.dtype.names:
349349
data_columns = list(data.dtype.names)
350-
data = dict((k, data[k]) for k in data_columns)
350+
data = {k: data[k] for k in data_columns}
351351
if columns is None:
352352
columns = data_columns
353353
mgr = self._init_dict(data, index, columns, dtype=dtype)
@@ -417,8 +417,7 @@ def _init_dict(self, data, index, columns, dtype=None):
417417
extract_index(list(data.values()))
418418

419419
# prefilter if columns passed
420-
data = dict((k, v) for k, v in compat.iteritems(data)
421-
if k in columns)
420+
data = {k: v for k, v in compat.iteritems(data) if k in columns}
422421

423422
if index is None:
424423
index = extract_index(list(data.values()))
@@ -3895,7 +3894,7 @@ def f(col):
38953894
return self._constructor_sliced(r, index=new_index,
38963895
dtype=r.dtype)
38973896

3898-
result = dict((col, f(col)) for col in this)
3897+
result = {col: f(col) for col in this}
38993898

39003899
# non-unique
39013900
else:
@@ -3906,7 +3905,7 @@ def f(i):
39063905
return self._constructor_sliced(r, index=new_index,
39073906
dtype=r.dtype)
39083907

3909-
result = dict((i, f(i)) for i, col in enumerate(this.columns))
3908+
result = {i: f(i) for i, col in enumerate(this.columns)}
39103909
result = self._constructor(result, index=new_index, copy=False)
39113910
result.columns = new_columns
39123911
return result
@@ -3984,7 +3983,7 @@ def _compare_frame_evaluate(self, other, func, str_rep, try_cast=True):
39843983
if self.columns.is_unique:
39853984

39863985
def _compare(a, b):
3987-
return dict((col, func(a[col], b[col])) for col in a.columns)
3986+
return {col: func(a[col], b[col]) for col in a.columns}
39883987

39893988
new_data = expressions.evaluate(_compare, str_rep, self, other)
39903989
return self._constructor(data=new_data, index=self.index,
@@ -3993,8 +3992,8 @@ def _compare(a, b):
39933992
else:
39943993

39953994
def _compare(a, b):
3996-
return dict((i, func(a.iloc[:, i], b.iloc[:, i]))
3997-
for i, col in enumerate(a.columns))
3995+
return {i: func(a.iloc[:, i], b.iloc[:, i])
3996+
for i, col in enumerate(a.columns)}
39983997

39993998
new_data = expressions.evaluate(_compare, str_rep, self, other)
40003999
result = self._constructor(data=new_data, index=self.index,

pandas/core/generic.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ def _setup_axes(cls, axes, info_axis=None, stat_axis=None, aliases=None,
235235
"""
236236

237237
cls._AXIS_ORDERS = axes
238-
cls._AXIS_NUMBERS = dict((a, i) for i, a in enumerate(axes))
238+
cls._AXIS_NUMBERS = {a: i for i, a in enumerate(axes)}
239239
cls._AXIS_LEN = len(axes)
240240
cls._AXIS_ALIASES = aliases or dict()
241-
cls._AXIS_IALIASES = dict((v, k) for k, v in cls._AXIS_ALIASES.items())
241+
cls._AXIS_IALIASES = {v: k for k, v in cls._AXIS_ALIASES.items()}
242242
cls._AXIS_NAMES = dict(enumerate(axes))
243243
cls._AXIS_SLICEMAP = slicers or None
244244
cls._AXIS_REVERSED = axes_are_reversed
@@ -279,21 +279,21 @@ def set_axis(a, i):
279279

280280
def _construct_axes_dict(self, axes=None, **kwargs):
281281
"""Return an axes dictionary for myself."""
282-
d = dict((a, self._get_axis(a)) for a in (axes or self._AXIS_ORDERS))
282+
d = {a: self._get_axis(a) for a in (axes or self._AXIS_ORDERS)}
283283
d.update(kwargs)
284284
return d
285285

286286
@staticmethod
287287
def _construct_axes_dict_from(self, axes, **kwargs):
288288
"""Return an axes dictionary for the passed axes."""
289-
d = dict((a, ax) for a, ax in zip(self._AXIS_ORDERS, axes))
289+
d = {a: ax for a, ax in zip(self._AXIS_ORDERS, axes)}
290290
d.update(kwargs)
291291
return d
292292

293293
def _construct_axes_dict_for_slice(self, axes=None, **kwargs):
294294
"""Return an axes dictionary for myself."""
295-
d = dict((self._AXIS_SLICEMAP[a], self._get_axis(a))
296-
for a in (axes or self._AXIS_ORDERS))
295+
d = {self._AXIS_SLICEMAP[a]: self._get_axis(a)
296+
for a in (axes or self._AXIS_ORDERS)}
297297
d.update(kwargs)
298298
return d
299299

@@ -329,7 +329,7 @@ def _construct_axes_from_arguments(self, args, kwargs, require_all=False):
329329
raise TypeError("not enough/duplicate arguments "
330330
"specified!")
331331

332-
axes = dict((a, kwargs.pop(a, None)) for a in self._AXIS_ORDERS)
332+
axes = {a: kwargs.pop(a, None) for a in self._AXIS_ORDERS}
333333
return axes, kwargs
334334

335335
@classmethod
@@ -1172,7 +1172,7 @@ def to_dense(self):
11721172
# Picklability
11731173

11741174
def __getstate__(self):
1175-
meta = dict((k, getattr(self, k, None)) for k in self._metadata)
1175+
meta = {k: getattr(self, k, None) for k in self._metadata}
11761176
return dict(_data=self._data, _typ=self._typ, _metadata=self._metadata,
11771177
**meta)
11781178

@@ -4277,8 +4277,8 @@ def fillna(self, value=None, method=None, axis=None, inplace=False,
42774277
elif self.ndim == 3:
42784278

42794279
# fill in 2d chunks
4280-
result = dict((col, s.fillna(method=method, value=value))
4281-
for col, s in self.iteritems())
4280+
result = {col: s.fillna(method=method, value=value)
4281+
for col, s in self.iteritems()}
42824282
new_obj = self._constructor.\
42834283
from_dict(result).__finalize__(self)
42844284
new_data = new_obj._data
@@ -5681,7 +5681,7 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
56815681
# this means other is a DataFrame, and we need to broadcast
56825682
# self
56835683
cons = self._constructor_expanddim
5684-
df = cons(dict((c, self) for c in other.columns),
5684+
df = cons({c: self for c in other.columns},
56855685
**other._construct_axes_dict())
56865686
return df._align_frame(other, join=join, axis=axis,
56875687
level=level, copy=copy,
@@ -5691,7 +5691,7 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
56915691
# this means self is a DataFrame, and we need to broadcast
56925692
# other
56935693
cons = other._constructor_expanddim
5694-
df = cons(dict((c, other) for c in self.columns),
5694+
df = cons({c: other for c in self.columns},
56955695
**self._construct_axes_dict())
56965696
return self._align_frame(df, join=join, axis=axis, level=level,
56975697
copy=copy, fill_value=fill_value,

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3840,7 +3840,7 @@ def first_not_none(values):
38403840
# if all series have a consistent name. If the
38413841
# series do not have a consistent name, do
38423842
# nothing.
3843-
names = set(v.name for v in values)
3843+
names = {v.name for v in values}
38443844
if len(names) == 1:
38453845
index.name = list(names)[0]
38463846

pandas/core/indexes/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ def _coerce_to_ndarray(cls, data):
732732

733733
def _get_attributes_dict(self):
734734
""" return an attributes dict for my class """
735-
return dict((k, getattr(self, k, None)) for k in self._attributes)
735+
return {k: getattr(self, k, None) for k in self._attributes}
736736

737737
def view(self, cls=None):
738738

@@ -1784,7 +1784,7 @@ def append(self, other):
17841784
if not isinstance(obj, Index):
17851785
raise TypeError('all inputs must be Index')
17861786

1787-
names = set(obj.name for obj in to_concat)
1787+
names = {obj.name for obj in to_concat}
17881788
name = None if len(names) > 1 else self.name
17891789

17901790
return self._concat(to_concat, name)

pandas/core/indexes/interval.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ def _concat_same_dtype(self, to_concat, name):
10241024
assert that we all have the same .closed
10251025
we allow a 0-len index here as well
10261026
"""
1027-
if not len(set(i.closed for i in to_concat if len(i))) == 1:
1027+
if not len({i.closed for i in to_concat if len(i)}) == 1:
10281028
msg = ('can only append two IntervalIndex objects '
10291029
'that are closed on the same side')
10301030
raise ValueError(msg)

pandas/core/internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3377,7 +3377,7 @@ def reduction(self, f, axis=0, consolidate=True, transposed=False,
33773377
blocks.append(block)
33783378

33793379
# note that some DatetimeTZ, Categorical are always ndim==1
3380-
ndim = set(b.ndim for b in blocks)
3380+
ndim = {b.ndim for b in blocks}
33813381

33823382
if 2 in ndim:
33833383

@@ -3891,7 +3891,7 @@ def get_scalar(self, tup):
38913891
"""
38923892
Retrieve single item
38933893
"""
3894-
full_loc = list(ax.get_loc(x) for ax, x in zip(self.axes, tup))
3894+
full_loc = [ax.get_loc(x) for ax, x in zip(self.axes, tup)]
38953895
blk = self.blocks[self._blknos[full_loc[0]]]
38963896
values = blk.values
38973897

@@ -4871,7 +4871,7 @@ def _merge_blocks(blocks, dtype=None, _can_consolidate=True):
48714871
if _can_consolidate:
48724872

48734873
if dtype is None:
4874-
if len(set(b.dtype for b in blocks)) != 1:
4874+
if len({b.dtype for b in blocks}) != 1:
48754875
raise AssertionError("_merge_blocks are invalid!")
48764876
dtype = blocks[0].dtype
48774877

pandas/core/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def names(x):
146146
construct_result=_construct_divmod_result,
147147
)
148148

149-
new_methods = dict((names(k), v) for k, v in new_methods.items())
149+
new_methods = {names(k): v for k, v in new_methods.items()}
150150
return new_methods
151151

152152

pandas/core/panelnd.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def _combine_with_constructor(self, other, func):
105105
new_axes.append(getattr(self, a).union(getattr(other, a)))
106106

107107
# reindex: could check that everything's the same size, but forget it
108-
d = dict((a, ax) for a, ax in zip(self._AXIS_ORDERS, new_axes))
108+
d = {a: ax for a, ax in zip(self._AXIS_ORDERS, new_axes)}
109109
d['copy'] = False
110110
this = self.reindex(**d)
111111
other = other.reindex(**d)

pandas/core/reshape/concat.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ def _make_concat_multiindex(indexes, keys, levels=None, names=None):
568568
names = list(names)
569569
else:
570570
# make sure that all of the passed indices have the same nlevels
571-
if not len(set(idx.nlevels for idx in indexes)) == 1:
571+
if not len({idx.nlevels for idx in indexes}) == 1:
572572
raise AssertionError("Cannot concat indices that do"
573573
" not have the same number of levels")
574574

pandas/core/reshape/melt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def lreshape(data, groups, dropna=True, label=None):
149149
for c in pivot_cols:
150150
mask &= notna(mdata[c])
151151
if not mask.all():
152-
mdata = dict((k, v[mask]) for k, v in compat.iteritems(mdata))
152+
mdata = {k: v[mask] for k, v in compat.iteritems(mdata)}
153153

154154
from pandas import DataFrame
155155
return DataFrame(mdata, columns=id_cols + pivot_cols)

pandas/core/sparse/frame.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ def _init_dict(self, data, index, columns, dtype=None):
131131
# pre-filter out columns if we passed it
132132
if columns is not None:
133133
columns = _ensure_index(columns)
134-
data = dict((k, v) for k, v in compat.iteritems(data)
135-
if k in columns)
134+
data = {k: v for k, v in compat.iteritems(data) if k in columns}
136135
else:
137136
columns = Index(_try_sort(list(data.keys())))
138137

@@ -173,7 +172,7 @@ def _init_matrix(self, data, index, columns, dtype=None):
173172
""" Init self from ndarray or list of lists """
174173
data = _prep_ndarray(data, copy=False)
175174
index, columns = self._prep_index(data, index, columns)
176-
data = dict((idx, data[:, i]) for i, idx in enumerate(columns))
175+
data = {idx: data[:, i] for i, idx in enumerate(columns)}
177176
return self._init_dict(data, index, columns, dtype)
178177

179178
def _init_spmatrix(self, data, index, columns, dtype=None,
@@ -307,7 +306,7 @@ def to_dense(self):
307306
-------
308307
df : DataFrame
309308
"""
310-
data = dict((k, v.to_dense()) for k, v in compat.iteritems(self))
309+
data = {k: v.to_dense() for k, v in compat.iteritems(self)}
311310
return DataFrame(data, index=self.index, columns=self.columns)
312311

313312
def _apply_columns(self, func):
@@ -697,7 +696,7 @@ def _reindex_columns(self, columns, method, copy, level, fill_value=None,
697696
raise NotImplementedError("'method' argument is not supported")
698697

699698
# TODO: fill value handling
700-
sdict = dict((k, v) for k, v in compat.iteritems(self) if k in columns)
699+
sdict = {k: v for k, v in compat.iteritems(self) if k in columns}
701700
return self._constructor(
702701
sdict, index=self.index, columns=columns,
703702
default_fill_value=self._default_fill_value).__finalize__(self)

0 commit comments

Comments
 (0)