Skip to content

Commit 0508d81

Browse files
mroeschkejreback
authored andcommitted
CLN: For loops, boolean conditions, misc. (#25206)
1 parent a4f5987 commit 0508d81

24 files changed

+57
-80
lines changed

pandas/core/arrays/categorical.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2167,8 +2167,7 @@ def _reverse_indexer(self):
21672167
r, counts = libalgos.groupsort_indexer(self.codes.astype('int64'),
21682168
categories.size)
21692169
counts = counts.cumsum()
2170-
result = [r[counts[indexer]:counts[indexer + 1]]
2171-
for indexer in range(len(counts) - 1)]
2170+
result = (r[start:end] for start, end in zip(counts, counts[1:]))
21722171
result = dict(zip(categories, result))
21732172
return result
21742173

pandas/core/arrays/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def _dt_array_cmp(cls, op):
128128
Wrap comparison operations to convert datetime-like to datetime64
129129
"""
130130
opname = '__{name}__'.format(name=op.__name__)
131-
nat_result = True if opname == '__ne__' else False
131+
nat_result = opname == '__ne__'
132132

133133
def wrapper(self, other):
134134
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):

pandas/core/arrays/integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ def cmp_method(self, other):
561561
else:
562562
mask = self._mask | mask
563563

564-
result[mask] = True if op_name == 'ne' else False
564+
result[mask] = op_name == 'ne'
565565
return result
566566

567567
name = '__{name}__'.format(name=op.__name__)

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _period_array_cmp(cls, op):
4646
Wrap comparison operations to convert Period-like to PeriodDtype
4747
"""
4848
opname = '__{name}__'.format(name=op.__name__)
49-
nat_result = True if opname == '__ne__' else False
49+
nat_result = opname == '__ne__'
5050

5151
def wrapper(self, other):
5252
op = getattr(self.asi8, opname)

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def _td_array_cmp(cls, op):
6262
Wrap comparison operations to convert timedelta-like to timedelta64
6363
"""
6464
opname = '__{name}__'.format(name=op.__name__)
65-
nat_result = True if opname == '__ne__' else False
65+
nat_result = opname == '__ne__'
6666

6767
def wrapper(self, other):
6868
if isinstance(other, (ABCDataFrame, ABCSeries, ABCIndexClass)):

pandas/core/computation/pytables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def evaluate(self):
252252
.format(slf=self))
253253

254254
rhs = self.conform(self.rhs)
255-
values = [TermValue(v, v, self.kind) for v in rhs]
255+
values = [TermValue(v, v, self.kind).value for v in rhs]
256256

257257
if self.is_in_table:
258258

@@ -263,7 +263,7 @@ def evaluate(self):
263263
self.filter = (
264264
self.lhs,
265265
filter_op,
266-
pd.Index([v.value for v in values]))
266+
pd.Index(values))
267267

268268
return self
269269
return None
@@ -275,7 +275,7 @@ def evaluate(self):
275275
self.filter = (
276276
self.lhs,
277277
filter_op,
278-
pd.Index([v.value for v in values]))
278+
pd.Index(values))
279279

280280
else:
281281
raise TypeError("passing a filterable condition to a non-table "

pandas/core/dtypes/cast.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1111,11 +1111,9 @@ def find_common_type(types):
11111111
# this is different from numpy, which casts bool with float/int as int
11121112
has_bools = any(is_bool_dtype(t) for t in types)
11131113
if has_bools:
1114-
has_ints = any(is_integer_dtype(t) for t in types)
1115-
has_floats = any(is_float_dtype(t) for t in types)
1116-
has_complex = any(is_complex_dtype(t) for t in types)
1117-
if has_ints or has_floats or has_complex:
1118-
return np.object
1114+
for t in types:
1115+
if is_integer_dtype(t) or is_float_dtype(t) or is_complex_dtype(t):
1116+
return np.object
11191117

11201118
return np.find_common_type(types, [])
11211119

pandas/core/dtypes/concat.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,6 @@ def is_nonempty(x):
123123
except Exception:
124124
return True
125125

126-
nonempty = [x for x in to_concat if is_nonempty(x)]
127-
128126
# If all arrays are empty, there's nothing to convert, just short-cut to
129127
# the concatenation, #3121.
130128
#
@@ -148,11 +146,11 @@ def is_nonempty(x):
148146
elif 'sparse' in typs:
149147
return _concat_sparse(to_concat, axis=axis, typs=typs)
150148

151-
extensions = [is_extension_array_dtype(x) for x in to_concat]
152-
if any(extensions) and axis == 1:
149+
all_empty = all(not is_nonempty(x) for x in to_concat)
150+
if any(is_extension_array_dtype(x) for x in to_concat) and axis == 1:
153151
to_concat = [np.atleast_2d(x.astype('object')) for x in to_concat]
154152

155-
if not nonempty:
153+
if all_empty:
156154
# we have all empties, but may need to coerce the result dtype to
157155
# object if we have non-numeric type operands (numpy would otherwise
158156
# cast this to float)

pandas/core/dtypes/dtypes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,7 @@ def _hash_categories(categories, ordered=True):
414414
cat_array = hash_tuples(categories)
415415
else:
416416
if categories.dtype == 'O':
417-
types = [type(x) for x in categories]
418-
if not len(set(types)) == 1:
417+
if len({type(x) for x in categories}) != 1:
419418
# TODO: hash_array doesn't handle mixed types. It casts
420419
# everything to a str first, which means we treat
421420
# {'1', '2'} the same as {'1', 2}

pandas/core/frame.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1535,8 +1535,8 @@ def from_records(cls, data, index=None, exclude=None, columns=None,
15351535
result_index = Index([], name=index)
15361536
else:
15371537
try:
1538-
to_remove = [arr_columns.get_loc(field) for field in index]
1539-
index_data = [arrays[i] for i in to_remove]
1538+
index_data = [arrays[arr_columns.get_loc(field)]
1539+
for field in index]
15401540
result_index = ensure_index_from_sequences(index_data,
15411541
names=index)
15421542

pandas/core/generic.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -1564,14 +1564,14 @@ def _is_label_reference(self, key, axis=0):
15641564
-------
15651565
is_label: bool
15661566
"""
1567-
axis = self._get_axis_number(axis)
1568-
other_axes = [ax for ax in range(self._AXIS_LEN) if ax != axis]
1569-
15701567
if self.ndim > 2:
15711568
raise NotImplementedError(
15721569
"_is_label_reference is not implemented for {type}"
15731570
.format(type=type(self)))
15741571

1572+
axis = self._get_axis_number(axis)
1573+
other_axes = (ax for ax in range(self._AXIS_LEN) if ax != axis)
1574+
15751575
return (key is not None and
15761576
is_hashable(key) and
15771577
any(key in self.axes[ax] for ax in other_axes))
@@ -1623,15 +1623,14 @@ def _check_label_or_level_ambiguity(self, key, axis=0):
16231623
------
16241624
ValueError: `key` is ambiguous
16251625
"""
1626-
1627-
axis = self._get_axis_number(axis)
1628-
other_axes = [ax for ax in range(self._AXIS_LEN) if ax != axis]
1629-
16301626
if self.ndim > 2:
16311627
raise NotImplementedError(
16321628
"_check_label_or_level_ambiguity is not implemented for {type}"
16331629
.format(type=type(self)))
16341630

1631+
axis = self._get_axis_number(axis)
1632+
other_axes = (ax for ax in range(self._AXIS_LEN) if ax != axis)
1633+
16351634
if (key is not None and
16361635
is_hashable(key) and
16371636
key in self.axes[axis].names and
@@ -1689,15 +1688,14 @@ def _get_label_or_level_values(self, key, axis=0):
16891688
if `key` is ambiguous. This will become an ambiguity error in a
16901689
future version
16911690
"""
1692-
1693-
axis = self._get_axis_number(axis)
1694-
other_axes = [ax for ax in range(self._AXIS_LEN) if ax != axis]
1695-
16961691
if self.ndim > 2:
16971692
raise NotImplementedError(
16981693
"_get_label_or_level_values is not implemented for {type}"
16991694
.format(type=type(self)))
17001695

1696+
axis = self._get_axis_number(axis)
1697+
other_axes = [ax for ax in range(self._AXIS_LEN) if ax != axis]
1698+
17011699
if self._is_label_reference(key, axis=axis):
17021700
self._check_label_or_level_ambiguity(key, axis=axis)
17031701
values = self.xs(key, axis=other_axes[0])._values
@@ -1753,14 +1751,13 @@ def _drop_labels_or_levels(self, keys, axis=0):
17531751
ValueError
17541752
if any `keys` match neither a label nor a level
17551753
"""
1756-
1757-
axis = self._get_axis_number(axis)
1758-
17591754
if self.ndim > 2:
17601755
raise NotImplementedError(
17611756
"_drop_labels_or_levels is not implemented for {type}"
17621757
.format(type=type(self)))
17631758

1759+
axis = self._get_axis_number(axis)
1760+
17641761
# Validate keys
17651762
keys = com.maybe_make_list(keys)
17661763
invalid_keys = [k for k in keys if not
@@ -8579,7 +8576,7 @@ def _where(self, cond, other=np.nan, inplace=False, axis=None, level=None,
85798576
cond = self._constructor(cond, **self._construct_axes_dict())
85808577

85818578
# make sure we are boolean
8582-
fill_value = True if inplace else False
8579+
fill_value = bool(inplace)
85838580
cond = cond.fillna(fill_value)
85848581

85858582
msg = "Boolean array expected for the condition, not {dtype}"
@@ -10243,8 +10240,8 @@ def last_valid_index(self):
1024310240

1024410241
def _doc_parms(cls):
1024510242
"""Return a tuple of the doc parms."""
10246-
axis_descr = "{%s}" % ', '.join(["{0} ({1})".format(a, i)
10247-
for i, a in enumerate(cls._AXIS_ORDERS)])
10243+
axis_descr = "{%s}" % ', '.join("{0} ({1})".format(a, i)
10244+
for i, a in enumerate(cls._AXIS_ORDERS))
1024810245
name = (cls._constructor_sliced.__name__
1024910246
if cls._AXIS_LEN > 1 else 'scalar')
1025010247
name2 = cls.__name__

pandas/core/groupby/generic.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,8 @@ def _reindex_output(self, result):
14621462
# reindex `result`, and then reset the in-axis grouper columns.
14631463

14641464
# Select in-axis groupers
1465-
in_axis_grps = [(i, ping.name) for (i, ping)
1466-
in enumerate(groupings) if ping.in_axis]
1465+
in_axis_grps = ((i, ping.name) for (i, ping)
1466+
in enumerate(groupings) if ping.in_axis)
14671467
g_nums, g_names = zip(*in_axis_grps)
14681468

14691469
result = result.drop(labels=list(g_names), axis=1)

pandas/core/groupby/groupby.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,12 @@ def get_converter(s):
443443
raise ValueError(msg)
444444

445445
converters = [get_converter(s) for s in index_sample]
446-
names = [tuple(f(n) for f, n in zip(converters, name))
447-
for name in names]
446+
names = (tuple(f(n) for f, n in zip(converters, name))
447+
for name in names)
448448

449449
else:
450450
converter = get_converter(index_sample)
451-
names = [converter(name) for name in names]
451+
names = (converter(name) for name in names)
452452

453453
return [self.indices.get(name, []) for name in names]
454454

pandas/core/groupby/grouper.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,9 @@ def groups(self):
195195
return self.grouper.groups
196196

197197
def __repr__(self):
198-
attrs_list = ["{}={!r}".format(attr_name, getattr(self, attr_name))
198+
attrs_list = ("{}={!r}".format(attr_name, getattr(self, attr_name))
199199
for attr_name in self._attributes
200-
if getattr(self, attr_name) is not None]
200+
if getattr(self, attr_name) is not None)
201201
attrs = ", ".join(attrs_list)
202202
cls_name = self.__class__.__name__
203203
return "{}({})".format(cls_name, attrs)

pandas/core/indexes/category.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,8 @@ def _concat_same_dtype(self, to_concat, name):
780780
Concatenate to_concat which has the same class
781781
ValueError if other is not in the categories
782782
"""
783-
to_concat = [self._is_dtype_compat(c) for c in to_concat]
784-
codes = np.concatenate([c.codes for c in to_concat])
783+
codes = np.concatenate([self._is_dtype_compat(c).codes
784+
for c in to_concat])
785785
result = self._create_from_codes(codes, name=name)
786786
# if name is None, _create_from_codes sets self.name
787787
result.name = name

pandas/core/indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ def _setitem_with_indexer(self, indexer, value):
347347
# must have all defined axes if we have a scalar
348348
# or a list-like on the non-info axes if we have a
349349
# list-like
350-
len_non_info_axes = [
350+
len_non_info_axes = (
351351
len(_ax) for _i, _ax in enumerate(self.obj.axes)
352352
if _i != i
353-
]
353+
)
354354
if any(not l for l in len_non_info_axes):
355355
if not is_list_like_indexer(value):
356356
raise ValueError("cannot set a frame with no "

pandas/core/internals/construction.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,12 @@ def init_dict(data, index, columns, dtype=None):
197197
arrays.loc[missing] = [val] * missing.sum()
198198

199199
else:
200-
201-
for key in data:
202-
if (isinstance(data[key], ABCDatetimeIndex) and
203-
data[key].tz is not None):
204-
# GH#24096 need copy to be deep for datetime64tz case
205-
# TODO: See if we can avoid these copies
206-
data[key] = data[key].copy(deep=True)
207-
208200
keys = com.dict_keys_to_ordered_list(data)
209201
columns = data_names = Index(keys)
210-
arrays = [data[k] for k in keys]
211-
202+
# GH#24096 need copy to be deep for datetime64tz case
203+
# TODO: See if we can avoid these copies
204+
arrays = [data[k] if not is_datetime64tz_dtype(data[k]) else
205+
data[k].copy(deep=True) for k in keys]
212206
return arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
213207

214208

pandas/core/resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ def __unicode__(self):
8383
"""
8484
Provide a nice str repr of our rolling object.
8585
"""
86-
attrs = ["{k}={v}".format(k=k, v=getattr(self.groupby, k))
86+
attrs = ("{k}={v}".format(k=k, v=getattr(self.groupby, k))
8787
for k in self._attributes if
88-
getattr(self.groupby, k, None) is not None]
88+
getattr(self.groupby, k, None) is not None)
8989
return "{klass} [{attrs}]".format(klass=self.__class__.__name__,
9090
attrs=', '.join(attrs))
9191

pandas/core/reshape/pivot.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
8888
# the original values are ints
8989
# as we grouped with a NaN value
9090
# and then dropped, coercing to floats
91-
for v in [v for v in values if v in data and v in agged]:
92-
if (is_integer_dtype(data[v]) and
93-
not is_integer_dtype(agged[v])):
91+
for v in values:
92+
if (v in data and is_integer_dtype(data[v]) and
93+
v in agged and not is_integer_dtype(agged[v])):
9494
agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)
9595

9696
table = agged

pandas/core/reshape/tile.py

-8
Original file line numberDiff line numberDiff line change
@@ -372,14 +372,6 @@ def _bins_to_cuts(x, bins, right=True, labels=None,
372372
return result, bins
373373

374374

375-
def _trim_zeros(x):
376-
while len(x) > 1 and x[-1] == '0':
377-
x = x[:-1]
378-
if len(x) > 1 and x[-1] == '.':
379-
x = x[:-1]
380-
return x
381-
382-
383375
def _coerce_to_type(x):
384376
"""
385377
if the passed data is of datetime/timedelta type,

pandas/core/strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ def _wrap_result(self, result, use_codes=True,
18721872

18731873
if expand is None:
18741874
# infer from ndim if expand is not specified
1875-
expand = False if result.ndim == 1 else True
1875+
expand = result.ndim != 1
18761876

18771877
elif expand is True and not isinstance(self._orig, Index):
18781878
# required when expand=True is explicitly specified

pandas/core/tools/numeric.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def to_numeric(arg, errors='raise', downcast=None):
138138
values = values.astype(np.int64)
139139
else:
140140
values = ensure_object(values)
141-
coerce_numeric = False if errors in ('ignore', 'raise') else True
141+
coerce_numeric = errors not in ('ignore', 'raise')
142142
values = lib.maybe_convert_numeric(values, set(),
143143
coerce_numeric=coerce_numeric)
144144

pandas/core/window.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ def __unicode__(self):
164164
Provide a nice str repr of our rolling object.
165165
"""
166166

167-
attrs = ["{k}={v}".format(k=k, v=getattr(self, k))
167+
attrs = ("{k}={v}".format(k=k, v=getattr(self, k))
168168
for k in self._attributes
169-
if getattr(self, k, None) is not None]
169+
if getattr(self, k, None) is not None)
170170
return "{klass} [{attrs}]".format(klass=self._window_type,
171171
attrs=','.join(attrs))
172172

pandas/tests/arrays/test_integer.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _compare_other(self, data, op_name, other):
339339
expected = pd.Series(op(data._data, other))
340340

341341
# fill the nan locations
342-
expected[data._mask] = True if op_name == '__ne__' else False
342+
expected[data._mask] = op_name == '__ne__'
343343

344344
tm.assert_series_equal(result, expected)
345345

@@ -351,7 +351,7 @@ def _compare_other(self, data, op_name, other):
351351
expected = op(expected, other)
352352

353353
# fill the nan locations
354-
expected[data._mask] = True if op_name == '__ne__' else False
354+
expected[data._mask] = op_name == '__ne__'
355355

356356
tm.assert_series_equal(result, expected)
357357

0 commit comments

Comments
 (0)