Skip to content

Commit 7263a95

Browse files
STY: Use strict arg in zip() in pandas/core/arrays (#62596)
1 parent db31f6a commit 7263a95

File tree

10 files changed

+23
-24
lines changed

10 files changed

+23
-24
lines changed

pandas/core/arrays/arrow/array.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ def _cmp_method(self, other, op) -> ArrowExtensionArray:
888888
boxed = self._box_pa(other)
889889
except pa.lib.ArrowInvalid:
890890
# e.g. GH#60228 [1, "b"] we have to operate pointwise
891-
res_values = [op(x, y) for x, y in zip(self, other)]
891+
res_values = [op(x, y) for x, y in zip(self, other, strict=True)]
892892
result = pa.array(res_values, type=pa.bool_(), from_pandas=True)
893893
else:
894894
rtype = boxed.type
@@ -2713,7 +2713,7 @@ def _str_extract(self, pat: str, flags: int = 0, expand: bool = True):
27132713
if expand:
27142714
return {
27152715
col: self._from_pyarrow_array(pc.struct_field(result, [i]))
2716-
for col, i in zip(groups, range(result.type.num_fields))
2716+
for col, i in zip(groups, range(result.type.num_fields), strict=True)
27172717
}
27182718
else:
27192719
return type(self)(pc.struct_field(result, [0]))

pandas/core/arrays/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2869,7 +2869,7 @@ def convert_values(param):
28692869

28702870
# If the operator is not defined for the underlying objects,
28712871
# a TypeError should be raised
2872-
res = [op(a, b) for (a, b) in zip(lvalues, rvalues)]
2872+
res = [op(a, b) for (a, b) in zip(lvalues, rvalues, strict=True)]
28732873

28742874
def _maybe_convert(arr):
28752875
if coerce_to_dtype:
@@ -2885,7 +2885,7 @@ def _maybe_convert(arr):
28852885
return res
28862886

28872887
if op.__name__ in {"divmod", "rdivmod"}:
2888-
a, b = zip(*res)
2888+
a, b = zip(*res, strict=True)
28892889
return _maybe_convert(a), _maybe_convert(b)
28902890

28912891
return _maybe_convert(res)

pandas/core/arrays/categorical.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from csv import QUOTE_NONNUMERIC
44
from functools import partial
5+
import itertools
56
import operator
67
from shutil import get_terminal_size
78
from typing import (
@@ -2429,8 +2430,8 @@ def _reverse_indexer(self) -> dict[Hashable, npt.NDArray[np.intp]]:
24292430
ensure_platform_int(self.codes), categories.size
24302431
)
24312432
counts = ensure_int64(counts).cumsum()
2432-
_result = (r[start:end] for start, end in zip(counts, counts[1:]))
2433-
return dict(zip(categories, _result))
2433+
_result = (r[start:end] for start, end in itertools.pairwise(counts))
2434+
return dict(zip(categories, _result, strict=True))
24342435

24352436
# ------------------------------------------------------------------
24362437
# Reductions
@@ -3165,5 +3166,8 @@ def factorize_from_iterables(iterables) -> tuple[list[np.ndarray], list[Index]]:
31653166
# For consistency, it should return two empty lists.
31663167
return [], []
31673168

3168-
codes, categories = zip(*(factorize_from_iterable(it) for it in iterables))
3169+
codes, categories = zip(
3170+
*(factorize_from_iterable(it) for it in iterables),
3171+
strict=True,
3172+
)
31693173
return list(codes), list(categories)

pandas/core/arrays/datetimelike.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ def _concat_same_type(
23742374
to_concat = [x for x in to_concat if len(x)]
23752375

23762376
if obj.freq is not None and all(x.freq == obj.freq for x in to_concat):
2377-
pairs = zip(to_concat[:-1], to_concat[1:])
2377+
pairs = zip(to_concat[:-1], to_concat[1:], strict=True)
23782378
if all(pair[0][-1] + obj.freq == pair[1][0] for pair in pairs):
23792379
new_freq = obj.freq
23802380
new_obj._freq = new_freq

pandas/core/arrays/interval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,7 @@ def to_tuples(self, na_tuple: bool = True) -> np.ndarray:
18931893
>>> idx.to_tuples()
18941894
Index([(0, 1), (1, 2)], dtype='object')
18951895
"""
1896-
tuples = com.asarray_tuplesafe(zip(self._left, self._right))
1896+
tuples = com.asarray_tuplesafe(zip(self._left, self._right, strict=True))
18971897
if not na_tuple:
18981898
# GH 18756
18991899
tuples = np.where(~self.isna(), tuples, np.nan)

pandas/core/arrays/masked.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ def __iter__(self) -> Iterator:
344344
yield val
345345
else:
346346
na_value = self.dtype.na_value
347-
for isna_, val in zip(self._mask, self._data):
347+
for isna_, val in zip(self._mask, self._data, strict=True):
348348
if isna_:
349349
yield na_value
350350
else:

pandas/core/arrays/period.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ def _range_from_fields(
14451445

14461446
freqstr = freq.freqstr
14471447
year, quarter = _make_field_arrays(year, quarter)
1448-
for y, q in zip(year, quarter):
1448+
for y, q in zip(year, quarter, strict=True):
14491449
calendar_year, calendar_month = parsing.quarter_to_myear(y, q, freqstr)
14501450
val = libperiod.period_ordinal(
14511451
calendar_year, calendar_month, 1, 1, 1, 1, 0, 0, base
@@ -1455,7 +1455,7 @@ def _range_from_fields(
14551455
freq = to_offset(freq, is_period=True)
14561456
base = libperiod.freq_to_dtype_code(freq)
14571457
arrays = _make_field_arrays(year, month, day, hour, minute, second)
1458-
for y, mth, d, h, mn, s in zip(*arrays):
1458+
for y, mth, d, h, mn, s in zip(*arrays, strict=True):
14591459
ordinals.append(libperiod.period_ordinal(y, mth, d, h, mn, s, 0, 0, base))
14601460

14611461
return np.array(ordinals, dtype=np.int64), freq

pandas/core/arrays/sparse/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,7 @@ def __array_ufunc__(self, ufunc: np.ufunc, method: str, *inputs, **kwargs):
17531753
self._simple_new(
17541754
sp_value, self.sp_index, SparseDtype(sp_value.dtype, fv)
17551755
)
1756-
for sp_value, fv in zip(sp_values, fill_value)
1756+
for sp_value, fv in zip(sp_values, fill_value, strict=True)
17571757
)
17581758
return arrays
17591759
elif method == "reduce":

pandas/core/arrays/timedeltas.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,9 @@ def __truediv__(self, other):
621621
if is_object_dtype(other.dtype):
622622
other = np.asarray(other)
623623
if self.ndim > 1:
624-
res_cols = [left / right for left, right in zip(self, other)]
624+
res_cols = [
625+
left / right for left, right in zip(self, other, strict=True)
626+
]
625627
res_cols2 = [x.reshape(1, -1) for x in res_cols]
626628
result = np.concatenate(res_cols2, axis=0)
627629
else:
@@ -670,7 +672,9 @@ def __floordiv__(self, other):
670672
elif is_object_dtype(other.dtype):
671673
other = np.asarray(other)
672674
if self.ndim > 1:
673-
res_cols = [left // right for left, right in zip(self, other)]
675+
res_cols = [
676+
left // right for left, right in zip(self, other, strict=True)
677+
]
674678
res_cols2 = [x.reshape(1, -1) for x in res_cols]
675679
result = np.concatenate(res_cols2, axis=0)
676680
else:

pyproject.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,6 @@ exclude = [
445445
"pandas/_config/config.py" = ["B905"]
446446
"pandas/conftest.py" = ["B905"]
447447
"pandas/core/array_algos/quantile.py" = ["B905"]
448-
"pandas/core/arrays/arrow/array.py" = ["B905"]
449-
"pandas/core/arrays/base.py" = ["B905"]
450-
"pandas/core/arrays/categorical.py" = ["B905"]
451-
"pandas/core/arrays/datetimelike.py" = ["B905"]
452-
"pandas/core/arrays/interval.py" = ["B905"]
453-
"pandas/core/arrays/masked.py" = ["B905"]
454-
"pandas/core/arrays/period.py" = ["B905"]
455-
"pandas/core/arrays/sparse/array.py" = ["B905"]
456-
"pandas/core/arrays/timedeltas.py" = ["B905"]
457448
"pandas/core/computation/align.py" = ["B905"]
458449
"pandas/core/computation/expr.py" = ["B905"]
459450
"pandas/core/computation/ops.py" = ["B905"]

0 commit comments

Comments
 (0)