Skip to content

Commit a057135

Browse files
authored
CLN refactor core/arrays (#37581)
1 parent 1c6cd01 commit a057135

File tree

9 files changed

+40
-60
lines changed

9 files changed

+40
-60
lines changed

pandas/core/arrays/base.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def astype(self, dtype, copy=True):
460460
if is_dtype_equal(dtype, self.dtype):
461461
if not copy:
462462
return self
463-
elif copy:
463+
else:
464464
return self.copy()
465465
if isinstance(dtype, StringDtype): # allow conversion to StringArrays
466466
return dtype.construct_array_type()._from_sequence(self, copy=False)
@@ -544,14 +544,13 @@ def argsort(
544544
ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
545545

546546
values = self._values_for_argsort()
547-
result = nargsort(
547+
return nargsort(
548548
values,
549549
kind=kind,
550550
ascending=ascending,
551551
na_position=na_position,
552552
mask=np.asarray(self.isna()),
553553
)
554-
return result
555554

556555
def argmin(self):
557556
"""
@@ -780,12 +779,12 @@ def equals(self, other: object) -> bool:
780779
boolean
781780
Whether the arrays are equivalent.
782781
"""
783-
if not type(self) == type(other):
782+
if type(self) != type(other):
784783
return False
785784
other = cast(ExtensionArray, other)
786785
if not is_dtype_equal(self.dtype, other.dtype):
787786
return False
788-
elif not len(self) == len(other):
787+
elif len(self) != len(other):
789788
return False
790789
else:
791790
equal_values = self == other

pandas/core/arrays/boolean.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ def coerce_to_array(
170170
values[~mask_values] = values_object[~mask_values].astype(bool)
171171

172172
# if the values were integer-like, validate it were actually 0/1's
173-
if inferred_dtype in integer_like:
174-
if not np.all(
173+
if (inferred_dtype in integer_like) and not (
174+
np.all(
175175
values[~mask_values].astype(float)
176176
== values_object[~mask_values].astype(float)
177-
):
178-
raise TypeError("Need to pass bool-like values")
177+
)
178+
):
179+
raise TypeError("Need to pass bool-like values")
179180

180181
if mask is None and mask_values is None:
181182
mask = np.zeros(len(values), dtype=bool)
@@ -193,9 +194,9 @@ def coerce_to_array(
193194
if mask_values is not None:
194195
mask = mask | mask_values
195196

196-
if not values.ndim == 1:
197+
if values.ndim != 1:
197198
raise ValueError("values must be a 1D list-like")
198-
if not mask.ndim == 1:
199+
if mask.ndim != 1:
199200
raise ValueError("mask must be a 1D list-like")
200201

201202
return values, mask
@@ -395,9 +396,8 @@ def astype(self, dtype, copy: bool = True) -> ArrayLike:
395396
self._data.astype(dtype.numpy_dtype), self._mask.copy(), copy=False
396397
)
397398
# for integer, error if there are missing values
398-
if is_integer_dtype(dtype):
399-
if self._hasna:
400-
raise ValueError("cannot convert NA to integer")
399+
if is_integer_dtype(dtype) and self._hasna:
400+
raise ValueError("cannot convert NA to integer")
401401
# for float dtype, ensure we use np.nan before casting (numpy cannot
402402
# deal with pd.NA)
403403
na_value = self._na_value
@@ -576,7 +576,7 @@ def _logical_method(self, other, op):
576576
elif isinstance(other, np.bool_):
577577
other = other.item()
578578

579-
if other_is_scalar and not (other is libmissing.NA or lib.is_bool(other)):
579+
if other_is_scalar and other is not libmissing.NA and not lib.is_bool(other):
580580
raise TypeError(
581581
"'other' should be pandas.NA or a bool. "
582582
f"Got {type(other).__name__} instead."

pandas/core/arrays/categorical.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,7 @@ def isna(self):
13141314
Categorical.notna : Boolean inverse of Categorical.isna.
13151315
13161316
"""
1317-
ret = self._codes == -1
1318-
return ret
1317+
return self._codes == -1
13191318

13201319
isnull = isna
13211320

@@ -1363,7 +1362,7 @@ def value_counts(self, dropna=True):
13631362
from pandas import CategoricalIndex, Series
13641363

13651364
code, cat = self._codes, self.categories
1366-
ncat, mask = len(cat), 0 <= code
1365+
ncat, mask = (len(cat), code >= 0)
13671366
ix, clean = np.arange(ncat), mask.all()
13681367

13691368
if dropna or clean:
@@ -1920,8 +1919,7 @@ def _reverse_indexer(self) -> Dict[Hashable, np.ndarray]:
19201919
)
19211920
counts = counts.cumsum()
19221921
_result = (r[start:end] for start, end in zip(counts, counts[1:]))
1923-
result = dict(zip(categories, _result))
1924-
return result
1922+
return dict(zip(categories, _result))
19251923

19261924
# ------------------------------------------------------------------
19271925
# Reductions

pandas/core/arrays/datetimelike.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,7 @@ def _time_shift(self, periods, freq=None):
10621062
if isinstance(freq, str):
10631063
freq = to_offset(freq)
10641064
offset = periods * freq
1065-
result = self + offset
1066-
return result
1065+
return self + offset
10671066

10681067
if periods == 0 or len(self) == 0:
10691068
# GH#14811 empty case
@@ -1533,10 +1532,9 @@ def _round(self, freq, mode, ambiguous, nonexistent):
15331532
self = cast("DatetimeArray", self)
15341533
naive = self.tz_localize(None)
15351534
result = naive._round(freq, mode, ambiguous, nonexistent)
1536-
aware = result.tz_localize(
1535+
return result.tz_localize(
15371536
self.tz, ambiguous=ambiguous, nonexistent=nonexistent
15381537
)
1539-
return aware
15401538

15411539
values = self.view("i8")
15421540
result = round_nsint64(values, mode, freq)

pandas/core/arrays/masked.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@ def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
8484
"mask should be boolean numpy array. Use "
8585
"the 'pd.array' function instead"
8686
)
87-
if not values.ndim == 1:
87+
if values.ndim != 1:
8888
raise ValueError("values must be a 1D array")
89-
if not mask.ndim == 1:
89+
if mask.ndim != 1:
9090
raise ValueError("mask must be a 1D array")
9191

9292
if copy:
@@ -209,7 +209,8 @@ def to_numpy(
209209
dtype = object
210210
if self._hasna:
211211
if (
212-
not (is_object_dtype(dtype) or is_string_dtype(dtype))
212+
not is_object_dtype(dtype)
213+
and not is_string_dtype(dtype)
213214
and na_value is libmissing.NA
214215
):
215216
raise ValueError(

pandas/core/arrays/numpy_.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,15 @@ def all(self, *, axis=None, out=None, keepdims=False, skipna=True):
281281

282282
def min(self, *, skipna: bool = True, **kwargs) -> Scalar:
283283
nv.validate_min((), kwargs)
284-
result = masked_reductions.min(
284+
return masked_reductions.min(
285285
values=self.to_numpy(), mask=self.isna(), skipna=skipna
286286
)
287-
return result
288287

289288
def max(self, *, skipna: bool = True, **kwargs) -> Scalar:
290289
nv.validate_max((), kwargs)
291-
result = masked_reductions.max(
290+
return masked_reductions.max(
292291
values=self.to_numpy(), mask=self.isna(), skipna=skipna
293292
)
294-
return result
295293

296294
def sum(self, *, axis=None, skipna=True, min_count=0, **kwargs) -> Scalar:
297295
nv.validate_sum((), kwargs)

pandas/core/arrays/period.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ def astype(self, dtype, copy: bool = True):
589589
if is_dtype_equal(dtype, self._dtype):
590590
if not copy:
591591
return self
592-
elif copy:
592+
else:
593593
return self.copy()
594594
if is_period_dtype(dtype):
595595
return self.asfreq(dtype.freq)
@@ -1080,11 +1080,9 @@ def _make_field_arrays(*fields):
10801080
elif length is None:
10811081
length = len(x)
10821082

1083-
arrays = [
1083+
return [
10841084
np.asarray(x)
10851085
if isinstance(x, (np.ndarray, list, ABCSeries))
10861086
else np.repeat(x, length)
10871087
for x in fields
10881088
]
1089-
1090-
return arrays

pandas/core/arrays/sparse/array.py

+11-21
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,8 @@ def __init__(
316316
raise Exception("must only pass scalars with an index")
317317

318318
if is_scalar(data):
319-
if index is not None:
320-
if data is None:
321-
data = np.nan
319+
if index is not None and data is None:
320+
data = np.nan
322321

323322
if index is not None:
324323
npoints = len(index)
@@ -575,8 +574,7 @@ def density(self):
575574
>>> s.density
576575
0.6
577576
"""
578-
r = float(self.sp_index.npoints) / float(self.sp_index.length)
579-
return r
577+
return float(self.sp_index.npoints) / float(self.sp_index.length)
580578

581579
@property
582580
def npoints(self) -> int:
@@ -736,25 +734,17 @@ def value_counts(self, dropna=True):
736734

737735
keys, counts = algos.value_counts_arraylike(self.sp_values, dropna=dropna)
738736
fcounts = self.sp_index.ngaps
739-
if fcounts > 0:
740-
if self._null_fill_value and dropna:
741-
pass
737+
if fcounts > 0 and (not self._null_fill_value or not dropna):
738+
mask = isna(keys) if self._null_fill_value else keys == self.fill_value
739+
if mask.any():
740+
counts[mask] += fcounts
742741
else:
743-
if self._null_fill_value:
744-
mask = isna(keys)
745-
else:
746-
mask = keys == self.fill_value
747-
748-
if mask.any():
749-
counts[mask] += fcounts
750-
else:
751-
keys = np.insert(keys, 0, self.fill_value)
752-
counts = np.insert(counts, 0, fcounts)
742+
keys = np.insert(keys, 0, self.fill_value)
743+
counts = np.insert(counts, 0, fcounts)
753744

754745
if not isinstance(keys, ABCIndexClass):
755746
keys = Index(keys)
756-
result = Series(counts, index=keys)
757-
return result
747+
return Series(counts, index=keys)
758748

759749
# --------
760750
# Indexing
@@ -1062,7 +1052,7 @@ def astype(self, dtype=None, copy=True):
10621052
if is_dtype_equal(dtype, self._dtype):
10631053
if not copy:
10641054
return self
1065-
elif copy:
1055+
else:
10661056
return self.copy()
10671057
dtype = self.dtype.update_dtype(dtype)
10681058
subtype = dtype._subtype_with_str

pandas/core/arrays/timedeltas.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,7 @@ def _from_sequence(
227227
data, inferred_freq = sequence_to_td64ns(data, copy=copy, unit=None)
228228
freq, _ = dtl.validate_inferred_freq(None, inferred_freq, False)
229229

230-
result = cls._simple_new(data, freq=freq)
231-
return result
230+
return cls._simple_new(data, freq=freq)
232231

233232
@classmethod
234233
def _from_sequence_not_strict(
@@ -334,10 +333,9 @@ def astype(self, dtype, copy: bool = True):
334333
if self._hasnans:
335334
# avoid double-copying
336335
result = self._data.astype(dtype, copy=False)
337-
values = self._maybe_mask_results(
336+
return self._maybe_mask_results(
338337
result, fill_value=None, convert="float64"
339338
)
340-
return values
341339
result = self._data.astype(dtype, copy=copy)
342340
return result.astype("i8")
343341
elif is_timedelta64_ns_dtype(dtype):

0 commit comments

Comments
 (0)