Skip to content

Commit fa09c6e

Browse files
authored
CLN refactor rest of core (#37586)
1 parent 4f8bb5e commit fa09c6e

14 files changed

+105
-145
lines changed

pandas/core/aggregation.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ def validate_func_kwargs(
388388
>>> validate_func_kwargs({'one': 'min', 'two': 'max'})
389389
(['one', 'two'], ['min', 'max'])
390390
"""
391-
no_arg_message = "Must provide 'func' or named aggregation **kwargs."
392391
tuple_given_message = "func is expected but received {} in **kwargs."
393392
columns = list(kwargs)
394393
func = []
@@ -397,6 +396,7 @@ def validate_func_kwargs(
397396
raise TypeError(tuple_given_message.format(type(col_func).__name__))
398397
func.append(col_func)
399398
if not columns:
399+
no_arg_message = "Must provide 'func' or named aggregation **kwargs."
400400
raise TypeError(no_arg_message)
401401
return columns, func
402402

@@ -499,14 +499,14 @@ def transform_dict_like(
499499
try:
500500
results[name] = transform(colg, how, 0, *args, **kwargs)
501501
except Exception as err:
502-
if (
503-
str(err) == "Function did not transform"
504-
or str(err) == "No transform functions were provided"
505-
):
502+
if str(err) in {
503+
"Function did not transform",
504+
"No transform functions were provided",
505+
}:
506506
raise err
507507

508508
# combine results
509-
if len(results) == 0:
509+
if not results:
510510
raise ValueError("Transform function failed")
511511
return concat(results, axis=1)
512512

pandas/core/algorithms.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,10 @@ def _ensure_data(
150150
from pandas import PeriodIndex
151151

152152
values = PeriodIndex(values)._data
153-
dtype = values.dtype
154153
elif is_timedelta64_dtype(values.dtype) or is_timedelta64_dtype(dtype):
155154
from pandas import TimedeltaIndex
156155

157156
values = TimedeltaIndex(values)._data
158-
dtype = values.dtype
159157
else:
160158
# Datetime
161159
if values.ndim > 1 and is_datetime64_ns_dtype(values.dtype):
@@ -169,8 +167,7 @@ def _ensure_data(
169167
from pandas import DatetimeIndex
170168

171169
values = DatetimeIndex(values)._data
172-
dtype = values.dtype
173-
170+
dtype = values.dtype
174171
return values.asi8, dtype
175172

176173
elif is_categorical_dtype(values.dtype) and (
@@ -875,10 +872,9 @@ def value_counts_arraylike(values, dropna: bool):
875872
keys, counts = f(values, dropna)
876873

877874
mask = isna(values)
878-
if not dropna and mask.any():
879-
if not isna(keys).any():
880-
keys = np.insert(keys, 0, np.NaN)
881-
counts = np.insert(counts, 0, mask.sum())
875+
if not dropna and mask.any() and not isna(keys).any():
876+
keys = np.insert(keys, 0, np.NaN)
877+
counts = np.insert(counts, 0, mask.sum())
882878

883879
keys = _reconstruct_data(keys, original.dtype, original)
884880

@@ -1741,9 +1737,8 @@ def take_nd(
17411737
dtype, fill_value = arr.dtype, arr.dtype.type()
17421738

17431739
flip_order = False
1744-
if arr.ndim == 2:
1745-
if arr.flags.f_contiguous:
1746-
flip_order = True
1740+
if arr.ndim == 2 and arr.flags.f_contiguous:
1741+
flip_order = True
17471742

17481743
if flip_order:
17491744
arr = arr.T
@@ -1915,8 +1910,7 @@ def searchsorted(arr, value, side="left", sorter=None) -> np.ndarray:
19151910
# and `value` is a pd.Timestamp, we may need to convert value
19161911
arr = ensure_wrapped_if_datetimelike(arr)
19171912

1918-
result = arr.searchsorted(value, side=side, sorter=sorter)
1919-
return result
1913+
return arr.searchsorted(value, side=side, sorter=sorter)
19201914

19211915

19221916
# ---- #

pandas/core/array_algos/replace.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def _check_comparison_types(
4949
if is_scalar(result) and isinstance(a, np.ndarray):
5050
type_names = [type(a).__name__, type(b).__name__]
5151

52-
if isinstance(a, np.ndarray):
53-
type_names[0] = f"ndarray(dtype={a.dtype})"
52+
type_names[0] = f"ndarray(dtype={a.dtype})"
5453

5554
raise TypeError(
5655
f"Cannot compare types {repr(type_names[0])} and {repr(type_names[1])}"

pandas/core/base.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,9 @@ def _try_aggregate_string_function(self, arg: str, *args, **kwargs):
321321
return f
322322

323323
f = getattr(np, arg, None)
324-
if f is not None:
325-
if hasattr(self, "__array__"):
326-
# in particular exclude Window
327-
return f(self, *args, **kwargs)
324+
if f is not None and hasattr(self, "__array__"):
325+
# in particular exclude Window
326+
return f(self, *args, **kwargs)
328327

329328
raise AttributeError(
330329
f"'{arg}' is not a valid function for '{type(self).__name__}' object"
@@ -1046,15 +1045,14 @@ def value_counts(
10461045
1.0 1
10471046
dtype: int64
10481047
"""
1049-
result = value_counts(
1048+
return value_counts(
10501049
self,
10511050
sort=sort,
10521051
ascending=ascending,
10531052
normalize=normalize,
10541053
bins=bins,
10551054
dropna=dropna,
10561055
)
1057-
return result
10581056

10591057
def unique(self):
10601058
values = self._values
@@ -1317,8 +1315,7 @@ def drop_duplicates(self, keep="first"):
13171315
duplicated = self.duplicated(keep=keep)
13181316
# pandas\core\base.py:1507: error: Value of type "IndexOpsMixin" is not
13191317
# indexable [index]
1320-
result = self[np.logical_not(duplicated)] # type: ignore[index]
1321-
return result
1318+
return self[~duplicated] # type: ignore[index]
13221319

13231320
def duplicated(self, keep="first"):
13241321
return duplicated(self._values, keep=keep)

pandas/core/construction.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -343,8 +343,7 @@ def array(
343343
elif is_timedelta64_ns_dtype(dtype):
344344
return TimedeltaArray._from_sequence(data, dtype=dtype, copy=copy)
345345

346-
result = PandasArray._from_sequence(data, dtype=dtype, copy=copy)
347-
return result
346+
return PandasArray._from_sequence(data, dtype=dtype, copy=copy)
348347

349348

350349
def extract_array(obj: object, extract_numpy: bool = False) -> Union[Any, ArrayLike]:

pandas/core/frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def _repr_fits_horizontal_(self, ignore_width: bool = False) -> bool:
770770
# and to_string on entire frame may be expensive
771771
d = self
772772

773-
if not (max_rows is None): # unlimited rows
773+
if max_rows is not None: # unlimited rows
774774
# min of two, where one may be None
775775
d = d.iloc[: min(max_rows, len(d))]
776776
else:
@@ -2029,10 +2029,10 @@ def to_records(
20292029
np.asarray(self.iloc[:, i]) for i in range(len(self.columns))
20302030
]
20312031

2032-
count = 0
20332032
index_names = list(self.index.names)
20342033

20352034
if isinstance(self.index, MultiIndex):
2035+
count = 0
20362036
for i, n in enumerate(index_names):
20372037
if n is None:
20382038
index_names[i] = f"level_{count}"
@@ -3334,7 +3334,7 @@ def _set_value(self, index, col, value, takeable: bool = False):
33343334
takeable : interpret the index/col as indexers, default False
33353335
"""
33363336
try:
3337-
if takeable is True:
3337+
if takeable:
33383338
series = self._ixs(col, axis=1)
33393339
series._set_value(index, value, takeable=True)
33403340
return
@@ -5041,7 +5041,7 @@ class max type
50415041

50425042
multi_col = isinstance(self.columns, MultiIndex)
50435043
for i, (lev, lab) in reversed(list(enumerate(to_insert))):
5044-
if not (level is None or i in level):
5044+
if level is not None and i not in level:
50455045
continue
50465046
name = names[i]
50475047
if multi_col:

pandas/core/generic.py

+44-47
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,7 @@ def droplevel(self: FrameOrSeries, level, axis=0) -> FrameOrSeries:
773773
"""
774774
labels = self._get_axis(axis)
775775
new_labels = labels.droplevel(level)
776-
result = self.set_axis(new_labels, axis=axis, inplace=False)
777-
return result
776+
return self.set_axis(new_labels, axis=axis, inplace=False)
778777

779778
def pop(self, item: Hashable) -> Union[Series, Any]:
780779
result = self[item]
@@ -1445,8 +1444,7 @@ def __invert__(self):
14451444
return self
14461445

14471446
new_data = self._mgr.apply(operator.invert)
1448-
result = self._constructor(new_data).__finalize__(self, method="__invert__")
1449-
return result
1447+
return self._constructor(new_data).__finalize__(self, method="__invert__")
14501448

14511449
@final
14521450
def __nonzero__(self):
@@ -2036,8 +2034,7 @@ def _repr_data_resource_(self):
20362034

20372035
as_json = data.to_json(orient="table")
20382036
as_json = cast(str, as_json)
2039-
payload = json.loads(as_json, object_pairs_hook=collections.OrderedDict)
2040-
return payload
2037+
return json.loads(as_json, object_pairs_hook=collections.OrderedDict)
20412038

20422039
# ----------------------------------------------------------------------
20432040
# I/O Methods
@@ -5342,11 +5339,11 @@ def sample(
53425339
"Replace has to be set to `True` when "
53435340
"upsampling the population `frac` > 1."
53445341
)
5345-
elif n is not None and frac is None and n % 1 != 0:
5342+
elif frac is None and n % 1 != 0:
53465343
raise ValueError("Only integers accepted as `n` values")
53475344
elif n is None and frac is not None:
53485345
n = round(frac * axis_length)
5349-
elif n is not None and frac is not None:
5346+
elif frac is not None:
53505347
raise ValueError("Please enter a value for `frac` OR `n`, not both")
53515348

53525349
# Check for negative sizes
@@ -5467,15 +5464,13 @@ def __getattr__(self, name: str):
54675464
# Note: obj.x will always call obj.__getattribute__('x') prior to
54685465
# calling obj.__getattr__('x').
54695466
if (
5470-
name in self._internal_names_set
5471-
or name in self._metadata
5472-
or name in self._accessors
5467+
name not in self._internal_names_set
5468+
and name not in self._metadata
5469+
and name not in self._accessors
5470+
and self._info_axis._can_hold_identifiers_and_holds_name(name)
54735471
):
5474-
return object.__getattribute__(self, name)
5475-
else:
5476-
if self._info_axis._can_hold_identifiers_and_holds_name(name):
5477-
return self[name]
5478-
return object.__getattribute__(self, name)
5472+
return self[name]
5473+
return object.__getattribute__(self, name)
54795474

54805475
def __setattr__(self, name: str, value) -> None:
54815476
"""
@@ -5585,17 +5580,16 @@ def _is_mixed_type(self) -> bool_t:
55855580
@final
55865581
def _check_inplace_setting(self, value) -> bool_t:
55875582
""" check whether we allow in-place setting with this type of value """
5588-
if self._is_mixed_type:
5589-
if not self._mgr.is_numeric_mixed_type:
5583+
if self._is_mixed_type and not self._mgr.is_numeric_mixed_type:
55905584

5591-
# allow an actual np.nan thru
5592-
if is_float(value) and np.isnan(value):
5593-
return True
5585+
# allow an actual np.nan thru
5586+
if is_float(value) and np.isnan(value):
5587+
return True
55945588

5595-
raise TypeError(
5596-
"Cannot do inplace boolean setting on "
5597-
"mixed-types with a non np.nan value"
5598-
)
5589+
raise TypeError(
5590+
"Cannot do inplace boolean setting on "
5591+
"mixed-types with a non np.nan value"
5592+
)
55995593

56005594
return True
56015595

@@ -6264,8 +6258,7 @@ def convert_dtypes(
62646258
)
62656259
for col_name, col in self.items()
62666260
]
6267-
result = concat(results, axis=1, copy=False)
6268-
return result
6261+
return concat(results, axis=1, copy=False)
62696262

62706263
# ----------------------------------------------------------------------
62716264
# Filling NA's
@@ -7443,9 +7436,13 @@ def clip(
74437436
upper = None
74447437

74457438
# GH 2747 (arguments were reversed)
7446-
if lower is not None and upper is not None:
7447-
if is_scalar(lower) and is_scalar(upper):
7448-
lower, upper = min(lower, upper), max(lower, upper)
7439+
if (
7440+
lower is not None
7441+
and upper is not None
7442+
and is_scalar(lower)
7443+
and is_scalar(upper)
7444+
):
7445+
lower, upper = min(lower, upper), max(lower, upper)
74497446

74507447
# fast-path for scalars
74517448
if (lower is None or (is_scalar(lower) and is_number(lower))) and (
@@ -8234,10 +8231,9 @@ def first(self: FrameOrSeries, offset) -> FrameOrSeries:
82348231
end_date = end = self.index[0] + offset
82358232

82368233
# Tick-like, e.g. 3 weeks
8237-
if isinstance(offset, Tick):
8238-
if end_date in self.index:
8239-
end = self.index.searchsorted(end_date, side="left")
8240-
return self.iloc[:end]
8234+
if isinstance(offset, Tick) and end_date in self.index:
8235+
end = self.index.searchsorted(end_date, side="left")
8236+
return self.iloc[:end]
82418237

82428238
return self.loc[:end]
82438239

@@ -8646,17 +8642,19 @@ def _align_frame(
86468642

86478643
is_series = isinstance(self, ABCSeries)
86488644

8649-
if axis is None or axis == 0:
8650-
if not self.index.equals(other.index):
8651-
join_index, ilidx, iridx = self.index.join(
8652-
other.index, how=join, level=level, return_indexers=True
8653-
)
8645+
if (axis is None or axis == 0) and not self.index.equals(other.index):
8646+
join_index, ilidx, iridx = self.index.join(
8647+
other.index, how=join, level=level, return_indexers=True
8648+
)
86548649

8655-
if axis is None or axis == 1:
8656-
if not is_series and not self.columns.equals(other.columns):
8657-
join_columns, clidx, cridx = self.columns.join(
8658-
other.columns, how=join, level=level, return_indexers=True
8659-
)
8650+
if (
8651+
(axis is None or axis == 1)
8652+
and not is_series
8653+
and not self.columns.equals(other.columns)
8654+
):
8655+
join_columns, clidx, cridx = self.columns.join(
8656+
other.columns, how=join, level=level, return_indexers=True
8657+
)
86608658

86618659
if is_series:
86628660
reindexers = {0: [join_index, ilidx]}
@@ -9524,9 +9522,8 @@ def truncate(
95249522
before = to_datetime(before)
95259523
after = to_datetime(after)
95269524

9527-
if before is not None and after is not None:
9528-
if before > after:
9529-
raise ValueError(f"Truncate: {after} must be after {before}")
9525+
if before is not None and after is not None and before > after:
9526+
raise ValueError(f"Truncate: {after} must be after {before}")
95309527

95319528
if len(ax) > 1 and ax.is_monotonic_decreasing:
95329529
before, after = after, before

pandas/core/indexers.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,11 @@ def is_scalar_indexer(indexer, ndim: int) -> bool:
8282
if ndim == 1 and is_integer(indexer):
8383
# GH37748: allow indexer to be an integer for Series
8484
return True
85-
if isinstance(indexer, tuple):
86-
if len(indexer) == ndim:
87-
return all(
88-
is_integer(x) or (isinstance(x, np.ndarray) and x.ndim == len(x) == 1)
89-
for x in indexer
90-
)
85+
if isinstance(indexer, tuple) and len(indexer) == ndim:
86+
return all(
87+
is_integer(x) or (isinstance(x, np.ndarray) and x.ndim == len(x) == 1)
88+
for x in indexer
89+
)
9190
return False
9291

9392

0 commit comments

Comments
 (0)