Skip to content

Commit d52e4aa

Browse files
committed
Zip Strict for pandas/core level files pandas-dev#62469
1 parent 8ea853a commit d52e4aa

File tree

8 files changed

+56
-44
lines changed

8 files changed

+56
-44
lines changed

pandas/core/apply.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ def compute_dict_like(
564564
indices = selected_obj.columns.get_indexer_for([key])
565565
labels = selected_obj.columns.take(indices)
566566
label_to_indices = defaultdict(list)
567-
for index, label in zip(indices, labels):
567+
for index, label in zip(indices, labels, strict=True):
568568
label_to_indices[label].append(index)
569569

570570
key_data = [
@@ -618,7 +618,9 @@ def wrap_results_dict_like(
618618
if all(is_ndframe):
619619
results = [result for result in result_data if not result.empty]
620620
keys_to_use: Iterable[Hashable]
621-
keys_to_use = [k for k, v in zip(result_index, result_data) if not v.empty]
621+
keys_to_use = [
622+
k for k, v in zip(result_index, result_data, strict=True) if not v.empty
623+
]
622624
# Have to check, if at least one DataFrame is not empty.
623625
if keys_to_use == []:
624626
keys_to_use = result_index
@@ -1359,7 +1361,7 @@ def series_generator(self) -> Generator[Series]:
13591361
yield obj._ixs(i, axis=0)
13601362

13611363
else:
1362-
for arr, name in zip(values, self.index):
1364+
for arr, name in zip(values, self.index, strict=True):
13631365
# GH#35462 re-pin mgr in case setitem changed it
13641366
ser._mgr = mgr
13651367
mgr.set_values(arr)
@@ -1913,7 +1915,7 @@ def relabel_result(
19131915
from pandas.core.indexes.base import Index
19141916

19151917
reordered_indexes = [
1916-
pair[0] for pair in sorted(zip(columns, order), key=lambda t: t[1])
1918+
pair[0] for pair in sorted(zip(columns, order, strict=True), key=lambda t: t[1])
19171919
]
19181920
reordered_result_in_dict: dict[Hashable, Series] = {}
19191921
idx = 0

pandas/core/arraylike.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,9 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
298298

299299
# align all the inputs.
300300
types = tuple(type(x) for x in inputs)
301-
alignable = [x for x, t in zip(inputs, types) if issubclass(t, NDFrame)]
301+
alignable = [
302+
x for x, t in zip(inputs, types, strict=True) if issubclass(t, NDFrame)
303+
]
302304

303305
if len(alignable) > 1:
304306
# This triggers alignment.
@@ -317,16 +319,16 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
317319
for obj in alignable[1:]:
318320
# this relies on the fact that we aren't handling mixed
319321
# series / frame ufuncs.
320-
for i, (ax1, ax2) in enumerate(zip(axes, obj.axes)):
322+
for i, (ax1, ax2) in enumerate(zip(axes, obj.axes, strict=True)):
321323
axes[i] = ax1.union(ax2)
322324

323-
reconstruct_axes = dict(zip(self._AXIS_ORDERS, axes))
325+
reconstruct_axes = dict(zip(self._AXIS_ORDERS, axes, strict=True))
324326
inputs = tuple(
325327
x.reindex(**reconstruct_axes) if issubclass(t, NDFrame) else x
326-
for x, t in zip(inputs, types)
328+
for x, t in zip(inputs, types, strict=True)
327329
)
328330
else:
329-
reconstruct_axes = dict(zip(self._AXIS_ORDERS, self.axes))
331+
reconstruct_axes = dict(zip(self._AXIS_ORDERS, self.axes, strict=True))
330332

331333
if self.ndim == 1:
332334
names = {x.name for x in inputs if hasattr(x, "name")}
@@ -450,7 +452,7 @@ def dispatch_ufunc_with_out(self, ufunc: np.ufunc, method: str, *inputs, **kwarg
450452
if not isinstance(out, tuple) or len(out) != len(result):
451453
raise NotImplementedError
452454

453-
for arr, res in zip(out, result):
455+
for arr, res in zip(out, result, strict=True):
454456
_assign_where(arr, res, where)
455457

456458
return out

pandas/core/frame.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ def iterrows(self) -> Iterable[tuple[Hashable, Series]]:
15241524
"""
15251525
columns = self.columns
15261526
klass = self._constructor_sliced
1527-
for k, v in zip(self.index, self.values):
1527+
for k, v in zip(self.index, self.values, strict=True):
15281528
s = klass(v, index=columns, name=k).__finalize__(self)
15291529
if self._mgr.is_single_block:
15301530
s._mgr.add_references(self._mgr)
@@ -1607,10 +1607,10 @@ def itertuples(
16071607
itertuple = collections.namedtuple( # type: ignore[misc]
16081608
name, fields, rename=True
16091609
)
1610-
return map(itertuple._make, zip(*arrays))
1610+
return map(itertuple._make, zip(*arrays, strict=True))
16111611

16121612
# fallback to regular tuples
1613-
return zip(*arrays)
1613+
return zip(*arrays, strict=True)
16141614

16151615
def __len__(self) -> int:
16161616
"""
@@ -4359,7 +4359,7 @@ def _setitem_array(self, key, value) -> None:
43594359

43604360
if isinstance(value, DataFrame):
43614361
check_key_length(self.columns, key, value)
4362-
for k1, k2 in zip(key, value.columns):
4362+
for k1, k2 in zip(key, value.columns, strict=False):
43634363
self[k1] = value[k2]
43644364

43654365
elif not is_list_like(value):
@@ -4465,7 +4465,7 @@ def _set_item_frame_value(self, key, value: DataFrame) -> None:
44654465
if len(cols_droplevel) and not cols_droplevel.equals(value.columns):
44664466
value = value.reindex(cols_droplevel, axis=1)
44674467

4468-
for col, col_droplevel in zip(cols, cols_droplevel):
4468+
for col, col_droplevel in zip(cols, cols_droplevel, strict=True):
44694469
self[col] = value[col_droplevel]
44704470
return
44714471

@@ -6567,7 +6567,11 @@ class max type
65676567
names = self.index._get_default_index_names(names, default)
65686568

65696569
if isinstance(self.index, MultiIndex):
6570-
to_insert = zip(reversed(self.index.levels), reversed(self.index.codes))
6570+
to_insert = zip(
6571+
reversed(self.index.levels),
6572+
reversed(self.index.codes),
6573+
strict=True,
6574+
)
65716575
else:
65726576
to_insert = ((self.index, None),)
65736577

@@ -7093,7 +7097,7 @@ def f(vals) -> tuple[np.ndarray, int]:
70937097
result.name = None
70947098
else:
70957099
vals = (col.values for name, col in self.items() if name in subset)
7096-
labels, shape = map(list, zip(*map(f, vals)))
7100+
labels, shape = map(list, zip(*map(f, vals), strict=True))
70977101

70987102
ids = get_group_index(labels, tuple(shape), sort=False, xnull=False)
70997103
result = self._constructor_sliced(duplicated(ids, keep), index=self.index)
@@ -7346,7 +7350,9 @@ def sort_values(
73467350

73477351
# need to rewrap columns in Series to apply key function
73487352
if key is not None:
7349-
keys_data = [Series(k, name=name) for (k, name) in zip(keys, by)]
7353+
keys_data = [
7354+
Series(k, name=name) for (k, name) in zip(keys, by, strict=True)
7355+
]
73507356
else:
73517357
# error: Argument 1 to "list" has incompatible type
73527358
# "Generator[ExtensionArray | ndarray[Any, Any], None, None]";
@@ -8208,7 +8214,7 @@ def _dispatch_frame_op(
82088214

82098215
arrays = [
82108216
array_op(_left, _right)
8211-
for _left, _right in zip(self._iter_column_arrays(), right)
8217+
for _left, _right in zip(self._iter_column_arrays(), right, strict=True)
82128218
]
82138219

82148220
elif isinstance(right, Series):
@@ -11745,7 +11751,7 @@ def c(x):
1174511751
return nanops.nancorr(x[0], x[1], method=method)
1174611752

1174711753
correl = self._constructor_sliced(
11748-
map(c, zip(left.values.T, right.values.T)),
11754+
map(c, zip(left.values.T, right.values.T, strict=True)),
1174911755
index=left.columns,
1175011756
copy=False,
1175111757
)

pandas/core/generic.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,12 @@ def _get_cleaned_column_resolvers(self) -> dict[Hashable, Series]:
614614
clean_column_name(k): Series(
615615
v, copy=False, index=self.index, name=k, dtype=dtype
616616
).__finalize__(self)
617-
for k, v, dtype in zip(self.columns, self._iter_column_arrays(), dtypes)
617+
for k, v, dtype in zip(
618+
self.columns,
619+
self._iter_column_arrays(),
620+
dtypes,
621+
strict=True,
622+
)
618623
}
619624

620625
@final
@@ -7546,7 +7551,7 @@ def replace(
75467551

75477552
items = list(to_replace.items())
75487553
if items:
7549-
keys, values = zip(*items)
7554+
keys, values = zip(*items, strict=True)
75507555
else:
75517556
keys, values = ([], []) # type: ignore[assignment]
75527557

@@ -7565,7 +7570,7 @@ def replace(
75657570
for k, v in items:
75667571
# error: Incompatible types in assignment (expression has type
75677572
# "list[Never]", variable has type "tuple[Any, ...]")
7568-
keys, values = list(zip(*v.items())) or ( # type: ignore[assignment]
7573+
keys, values = list(zip(*v.items(), strict=True)) or ( # type: ignore[assignment]
75697574
[],
75707575
[],
75717576
)

pandas/core/indexing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ def _getitem_lowerdim(self, tup: tuple):
10911091

10921092
# Reverse tuple so that we are indexing along columns before rows
10931093
# and avoid unintended dtype inference. # GH60600
1094-
for i, key in zip(range(len(tup) - 1, -1, -1), reversed(tup)):
1094+
for i, key in zip(range(len(tup) - 1, -1, -1), reversed(tup), strict=True):
10951095
if is_label_like(key) or is_list_like(key):
10961096
# We don't need to check for tuples here because those are
10971097
# caught by the _is_nested_tuple_indexer check above.
@@ -1357,7 +1357,7 @@ def _multi_take(self, tup: tuple):
13571357
# GH 836
13581358
d = {
13591359
axis: self._get_listlike_indexer(key, axis)
1360-
for (key, axis) in zip(tup, self.obj._AXIS_ORDERS)
1360+
for (key, axis) in zip(tup, self.obj._AXIS_ORDERS, strict=True)
13611361
}
13621362
return self.obj._reindex_with_indexers(d, allow_dups=True)
13631363

@@ -1669,7 +1669,7 @@ def _has_valid_setitem_indexer(self, indexer) -> bool:
16691669
if not isinstance(indexer, tuple):
16701670
indexer = _tuplify(self.ndim, indexer)
16711671

1672-
for ax, i in zip(self.obj.axes, indexer):
1672+
for ax, i in zip(self.obj.axes, indexer, strict=False):
16731673
if isinstance(i, slice):
16741674
# should check the stop slice?
16751675
pass
@@ -1841,7 +1841,7 @@ def _decide_split_path(self, indexer, value) -> bool:
18411841
# (not null slices) then we must take the split path, xref
18421842
# GH 10360, GH 27841
18431843
if isinstance(indexer, tuple) and len(indexer) == len(self.obj.axes):
1844-
for i, ax in zip(indexer, self.obj.axes):
1844+
for i, ax in zip(indexer, self.obj.axes, strict=True):
18451845
if isinstance(ax, MultiIndex) and not (
18461846
is_integer(i) or com.is_null_slice(i)
18471847
):
@@ -2036,7 +2036,7 @@ def _setitem_with_indexer_split_path(self, indexer, value, name: str):
20362036

20372037
elif len(ilocs) == len(value):
20382038
# We are setting multiple columns in a single row.
2039-
for loc, v in zip(ilocs, value):
2039+
for loc, v in zip(ilocs, value, strict=True):
20402040
self._setitem_single_column(loc, v, pi)
20412041

20422042
elif len(ilocs) == 1 and com.is_null_slice(pi) and len(self.obj) == 0:

pandas/core/series.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,7 @@ def items(self) -> Iterable[tuple[Hashable, Any]]:
17071707
Index : 1, Value : B
17081708
Index : 2, Value : C
17091709
"""
1710-
return zip(iter(self.index), iter(self))
1710+
return zip(iter(self.index), iter(self), strict=True)
17111711

17121712
# ----------------------------------------------------------------------
17131713
# Misc public methods
@@ -5505,12 +5505,12 @@ def case_when(
55055505
for condition, replacement in caselist
55065506
]
55075507
default = self.copy(deep=False)
5508-
conditions, replacements = zip(*caselist)
5508+
conditions, replacements = zip(*caselist, strict=True)
55095509
common_dtypes = [infer_dtype_from(arg)[0] for arg in [*replacements, default]]
55105510
if len(set(common_dtypes)) > 1:
55115511
common_dtype = find_common_type(common_dtypes)
55125512
updated_replacements = []
5513-
for condition, replacement in zip(conditions, replacements):
5513+
for condition, replacement in zip(conditions, replacements, strict=True):
55145514
if is_scalar(replacement):
55155515
replacement = construct_1d_arraylike_from_scalar(
55165516
value=replacement, length=len(condition), dtype=common_dtype
@@ -5525,7 +5525,7 @@ def case_when(
55255525

55265526
counter = range(len(conditions) - 1, -1, -1)
55275527
for position, condition, replacement in zip(
5528-
counter, reversed(conditions), reversed(replacements)
5528+
counter, reversed(conditions), reversed(replacements), strict=True
55295529
):
55305530
try:
55315531
default = default.mask(

pandas/core/sorting.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def maybe_lift(lab, size: int) -> tuple[np.ndarray, int]:
169169
labels = [ensure_int64(x) for x in labels]
170170
lshape = list(shape)
171171
if not xnull:
172-
for i, (lab, size) in enumerate(zip(labels, shape)):
172+
for i, (lab, size) in enumerate(zip(labels, shape, strict=True)):
173173
labels[i], lshape[i] = maybe_lift(lab, size)
174174

175175
# Iteratively process all the labels in chunks sized so less
@@ -289,7 +289,11 @@ def decons_obs_group_ids(
289289
if not is_int64_overflow_possible(shape):
290290
# obs ids are deconstructable! take the fast route!
291291
out = _decons_group_index(obs_ids, shape)
292-
return out if xnull or not lift.any() else [x - y for x, y in zip(out, lift)]
292+
return (
293+
out
294+
if xnull or not lift.any()
295+
else [x - y for x, y in zip(out, lift, strict=True)]
296+
)
293297

294298
indexer = unique_label_indices(comp_ids)
295299
return [lab[indexer].astype(np.intp, subok=False, copy=True) for lab in labels]
@@ -341,7 +345,7 @@ def lexsort_indexer(
341345

342346
labels = []
343347

344-
for k, order in zip(reversed(keys), orders):
348+
for k, order in zip(reversed(keys), orders, strict=True):
345349
k = ensure_key_mapped(k, key)
346350
if codes_given:
347351
codes = cast(np.ndarray, k)
@@ -473,9 +477,9 @@ def nargminmax(values: ExtensionArray, method: str, axis: AxisInt = 0):
473477
if arr_values.ndim > 1:
474478
if mask.any():
475479
if axis == 1:
476-
zipped = zip(arr_values, mask)
480+
zipped = zip(arr_values, mask, strict=True)
477481
else:
478-
zipped = zip(arr_values.T, mask.T)
482+
zipped = zip(arr_values.T, mask.T, strict=True)
479483
return np.array([_nanargminmax(v, m, func) for v, m in zipped])
480484
return func(arr_values, axis=axis)
481485

pyproject.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,7 @@ exclude = [
440440
"asv_bench/benchmarks/series_methods.py" = ["B905"]
441441
"pandas/_config/config.py" = ["B905"]
442442
"pandas/conftest.py" = ["B905"]
443-
"pandas/core/apply.py" = ["B905"]
444443
"pandas/core/array_algos/quantile.py" = ["B905"]
445-
"pandas/core/arraylike.py" = ["B905"]
446444
"pandas/core/arrays/arrow/array.py" = ["B905"]
447445
"pandas/core/arrays/base.py" = ["B905"]
448446
"pandas/core/arrays/categorical.py" = ["B905"]
@@ -456,24 +454,19 @@ exclude = [
456454
"pandas/core/computation/expr.py" = ["B905"]
457455
"pandas/core/computation/ops.py" = ["B905"]
458456
"pandas/core/dtypes/missing.py" = ["B905"]
459-
"pandas/core/frame.py" = ["B905"]
460-
"pandas/core/generic.py" = ["B905"]
461457
"pandas/core/groupby/generic.py" = ["B905"]
462458
"pandas/core/groupby/groupby.py" = ["B905"]
463459
"pandas/core/groupby/grouper.py" = ["B905"]
464460
"pandas/core/groupby/ops.py" = ["B905"]
465461
"pandas/core/indexes/interval.py" = ["B905"]
466462
"pandas/core/indexes/multi.py" = ["B905"]
467-
"pandas/core/indexing.py" = ["B905"]
468463
"pandas/core/methods/to_dict.py" = ["B905"]
469464
"pandas/core/reshape/concat.py" = ["B905"]
470465
"pandas/core/reshape/encoding.py" = ["B905"]
471466
"pandas/core/reshape/melt.py" = ["B905"]
472467
"pandas/core/reshape/merge.py" = ["B905"]
473468
"pandas/core/reshape/pivot.py" = ["B905"]
474469
"pandas/core/reshape/reshape.py" = ["B905"]
475-
"pandas/core/series.py" = ["B905"]
476-
"pandas/core/sorting.py" = ["B905"]
477470
"pandas/core/strings/accessor.py" = ["B905"]
478471
"pandas/core/window/rolling.py" = ["B905"]
479472
"pandas/io/excel/_xlrd.py" = ["B905"]

0 commit comments

Comments
 (0)