Skip to content

Commit 0508d81

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

File tree

24 files changed

+57
-80
lines changed

24 files changed

+57
-80
lines changed

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 3 additions & 5 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)