Skip to content

Commit f570026

Browse files
authored
STY: ZIP strict for pandas/io (#62469)
1 parent f440345 commit f570026

19 files changed

+88
-76
lines changed

pandas/io/excel/_xlrd.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ def _parse_cell(cell_contents, cell_typ):
134134
return [
135135
[
136136
_parse_cell(value, typ)
137-
for value, typ in zip(sheet.row_values(i), sheet.row_types(i))
137+
for value, typ in zip(
138+
sheet.row_values(i), sheet.row_types(i), strict=True
139+
)
138140
]
139141
for i in range(nrows)
140142
]

pandas/io/formats/css.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def expand(self: CSSResolver, prop: str, value: str) -> Generator[tuple[str, str
5757
stacklevel=find_stack_level(),
5858
)
5959
return
60-
for key, idx in zip(self.SIDES, mapping):
60+
for key, idx in zip(self.SIDES, mapping, strict=True):
6161
yield prop_fmt.format(key), tokens[idx]
6262

6363
return expand

pandas/io/formats/excel.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ def _format_header_mi(self) -> Iterable[ExcelCell]:
633633
)
634634

635635
for lnum, (spans, levels, level_codes) in enumerate(
636-
zip(level_lengths, columns.levels, columns.codes)
636+
zip(level_lengths, columns.levels, columns.codes, strict=True)
637637
):
638638
values = levels.take(level_codes)
639639
for i, span_val in spans.items():
@@ -792,7 +792,10 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]:
792792
level_lengths = get_level_lengths(level_strs)
793793

794794
for spans, levels, level_codes in zip(
795-
level_lengths, self.df.index.levels, self.df.index.codes
795+
level_lengths,
796+
self.df.index.levels,
797+
self.df.index.codes,
798+
strict=False,
796799
):
797800
values = levels.take(
798801
level_codes,
@@ -824,7 +827,7 @@ def _format_hierarchical_rows(self) -> Iterable[ExcelCell]:
824827

825828
else:
826829
# Format hierarchical rows with non-merged values.
827-
for indexcolvals in zip(*self.df.index):
830+
for indexcolvals in zip(*self.df.index, strict=True):
828831
for idx, indexcolval in enumerate(indexcolvals):
829832
# GH#60099
830833
if isinstance(indexcolval, Period):

pandas/io/formats/format.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ def _initialize_colspace(self, col_space: ColspaceArgType | None) -> ColspaceTyp
579579
f"Col_space length({len(col_space)}) should match "
580580
f"DataFrame number of columns({len(self.frame.columns)})"
581581
)
582-
result = dict(zip(self.frame.columns, col_space))
582+
result = dict(zip(self.frame.columns, col_space, strict=True))
583583
return result
584584

585585
def _calc_max_cols_fitted(self) -> int | None:
@@ -786,7 +786,7 @@ def _get_formatted_column_labels(self, frame: DataFrame) -> list[list[str]]:
786786
if self.sparsify and len(fmt_columns):
787787
fmt_columns = sparsify_labels(fmt_columns)
788788

789-
str_columns = [list(x) for x in zip(*fmt_columns)]
789+
str_columns = [list(x) for x in zip(*fmt_columns, strict=True)]
790790
else:
791791
fmt_columns = columns._format_flat(include_name=False)
792792
str_columns = [
@@ -795,7 +795,9 @@ def _get_formatted_column_labels(self, frame: DataFrame) -> list[list[str]]:
795795
if not self._get_formatter(i) and is_numeric_dtype(dtype)
796796
else x
797797
]
798-
for i, (x, dtype) in enumerate(zip(fmt_columns, self.frame.dtypes))
798+
for i, (x, dtype) in enumerate(
799+
zip(fmt_columns, self.frame.dtypes, strict=False)
800+
)
799801
]
800802
return str_columns
801803

@@ -1359,7 +1361,7 @@ def format_with_na_rep(
13591361
formatted = np.array(
13601362
[
13611363
formatter(val) if not m else na_rep
1362-
for val, m in zip(values.ravel(), mask.ravel())
1364+
for val, m in zip(values.ravel(), mask.ravel(), strict=True)
13631365
]
13641366
).reshape(values.shape)
13651367
return formatted
@@ -1377,6 +1379,7 @@ def format_complex_with_na_rep(
13771379
imag_values,
13781380
real_mask,
13791381
imag_mask,
1382+
strict=True,
13801383
):
13811384
if not re_isna and not im_isna:
13821385
formatted_lst.append(formatter(val))
@@ -1796,7 +1799,7 @@ def _trim_zeros_complex(str_complexes: ArrayLike, decimal: str = ".") -> list[st
17961799
+ imag_pt[0] # +/-
17971800
+ f"{imag_pt[1:]:>{padded_length}}" # complex part (no sign), possibly nan
17981801
+ "j"
1799-
for real_pt, imag_pt in zip(padded_parts[:n], padded_parts[n:])
1802+
for real_pt, imag_pt in zip(padded_parts[:n], padded_parts[n:], strict=True)
18001803
]
18011804
return padded
18021805

pandas/io/formats/html.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,9 @@ def _write_col_header(self, indent: int) -> None:
289289
levels = self.columns._format_multi(sparsify=sentinel, include_names=False)
290290
level_lengths = get_level_lengths(levels, sentinel)
291291
inner_lvl = len(level_lengths) - 1
292-
for lnum, (records, values) in enumerate(zip(level_lengths, levels)):
292+
for lnum, (records, values) in enumerate(
293+
zip(level_lengths, levels, strict=True)
294+
):
293295
if is_truncated_horizontally:
294296
# modify the header lines
295297
ins_col = self.fmt.tr_col_num
@@ -486,7 +488,7 @@ def _write_hierarchical_rows(
486488

487489
assert isinstance(frame.index, MultiIndex)
488490
idx_values = frame.index._format_multi(sparsify=False, include_names=False)
489-
idx_values = list(zip(*idx_values))
491+
idx_values = list(zip(*idx_values, strict=True))
490492

491493
if self.fmt.sparsify:
492494
# GH3547
@@ -547,7 +549,7 @@ def _write_hierarchical_rows(
547549

548550
sparse_offset = 0
549551
j = 0
550-
for records, v in zip(level_lengths, idx_values[i]):
552+
for records, v in zip(level_lengths, idx_values[i], strict=True):
551553
if i in records:
552554
if records[i] > 1:
553555
tags[j] = template.format(span=records[i])
@@ -584,7 +586,10 @@ def _write_hierarchical_rows(
584586
)
585587

586588
idx_values = list(
587-
zip(*frame.index._format_multi(sparsify=False, include_names=False))
589+
zip(
590+
*frame.index._format_multi(sparsify=False, include_names=False),
591+
strict=True,
592+
)
588593
)
589594
row = []
590595
row.extend(idx_values[i])

pandas/io/formats/info.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -868,12 +868,14 @@ def _get_gross_column_widths(self) -> Sequence[int]:
868868
body_column_widths = self._get_body_column_widths()
869869
return [
870870
max(*widths)
871-
for widths in zip(self.header_column_widths, body_column_widths)
871+
for widths in zip(
872+
self.header_column_widths, body_column_widths, strict=False
873+
)
872874
]
873875

874876
def _get_body_column_widths(self) -> Sequence[int]:
875877
"""Get widths of table content columns."""
876-
strcols: Sequence[Sequence[str]] = list(zip(*self.strrows))
878+
strcols: Sequence[Sequence[str]] = list(zip(*self.strrows, strict=True))
877879
return [max(len(x) for x in col) for col in strcols]
878880

879881
def _gen_rows(self) -> Iterator[Sequence[str]]:
@@ -899,7 +901,9 @@ def add_header_line(self) -> None:
899901
header_line = self.SPACING.join(
900902
[
901903
_put_str(header, col_width)
902-
for header, col_width in zip(self.headers, self.gross_column_widths)
904+
for header, col_width in zip(
905+
self.headers, self.gross_column_widths, strict=True
906+
)
903907
]
904908
)
905909
self._lines.append(header_line)
@@ -909,7 +913,7 @@ def add_separator_line(self) -> None:
909913
[
910914
_put_str("-" * header_colwidth, gross_colwidth)
911915
for header_colwidth, gross_colwidth in zip(
912-
self.header_column_widths, self.gross_column_widths
916+
self.header_column_widths, self.gross_column_widths, strict=True
913917
)
914918
]
915919
)
@@ -920,7 +924,9 @@ def add_body_lines(self) -> None:
920924
body_line = self.SPACING.join(
921925
[
922926
_put_str(col, gross_colwidth)
923-
for col, gross_colwidth in zip(row, self.gross_column_widths)
927+
for col, gross_colwidth in zip(
928+
row, self.gross_column_widths, strict=True
929+
)
924930
]
925931
)
926932
self._lines.append(body_line)
@@ -980,6 +986,7 @@ def _gen_rows_without_counts(self) -> Iterator[Sequence[str]]:
980986
self._gen_line_numbers(),
981987
self._gen_columns(),
982988
self._gen_dtypes(),
989+
strict=True,
983990
)
984991

985992
def _gen_rows_with_counts(self) -> Iterator[Sequence[str]]:
@@ -989,6 +996,7 @@ def _gen_rows_with_counts(self) -> Iterator[Sequence[str]]:
989996
self._gen_columns(),
990997
self._gen_non_null_counts(),
991998
self._gen_dtypes(),
999+
strict=True,
9921000
)
9931001

9941002
def _gen_line_numbers(self) -> Iterator[str]:
@@ -1092,10 +1100,7 @@ def _gen_rows_without_counts(self) -> Iterator[Sequence[str]]:
10921100

10931101
def _gen_rows_with_counts(self) -> Iterator[Sequence[str]]:
10941102
"""Iterator with string representation of body data with counts."""
1095-
yield from zip(
1096-
self._gen_non_null_counts(),
1097-
self._gen_dtypes(),
1098-
)
1103+
yield from zip(self._gen_non_null_counts(), self._gen_dtypes(), strict=True)
10991104

11001105

11011106
def _get_dataframe_dtype_counts(df: DataFrame) -> Mapping[str, int]:

pandas/io/formats/printing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def adjoin(space: int, *lists: list[str], **kwargs: Any) -> str:
6060
nl = justfunc(lst, lengths[i], mode="left")
6161
nl = ([" " * lengths[i]] * (maxLen - len(lst))) + nl
6262
newLists.append(nl)
63-
toJoin = zip(*newLists)
63+
toJoin = zip(*newLists, strict=True)
6464
return "\n".join("".join(lines) for lines in toJoin)
6565

6666

@@ -497,14 +497,16 @@ def _justify(
497497
max_length = [0] * len(combined[0])
498498
for inner_seq in combined:
499499
length = [len(item) for item in inner_seq]
500-
max_length = [max(x, y) for x, y in zip(max_length, length)]
500+
max_length = [max(x, y) for x, y in zip(max_length, length, strict=True)]
501501

502502
# justify each item in each list-like in head and tail using max_length
503503
head_tuples = [
504-
tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length)) for seq in head
504+
tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length, strict=True))
505+
for seq in head
505506
]
506507
tail_tuples = [
507-
tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length)) for seq in tail
508+
tuple(x.rjust(max_len) for x, max_len in zip(seq, max_length, strict=True))
509+
for seq in tail
508510
]
509511
return head_tuples, tail_tuples
510512

pandas/io/formats/style_render.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ def _translate_header(self, sparsify_cols: bool, max_cols: int):
429429
clabels = self.data.columns.tolist()
430430
if self.data.columns.nlevels == 1:
431431
clabels = [[x] for x in clabels]
432-
clabels = list(zip(*clabels))
432+
clabels = list(zip(*clabels, strict=True))
433433

434434
head = []
435435
# 1) column headers
@@ -914,7 +914,7 @@ def concatenated_visible_rows(obj):
914914
return row_indices
915915

916916
body = []
917-
for r, row in zip(concatenated_visible_rows(self), d["body"]):
917+
for r, row in zip(concatenated_visible_rows(self), d["body"], strict=True):
918918
# note: cannot enumerate d["body"] because rows were dropped if hidden
919919
# during _translate_body so must zip to acquire the true r-index associated
920920
# with the ctx obj which contains the cell styles.

pandas/io/json/_normalize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
550550
data = [data]
551551
if len(path) > 1:
552552
for obj in data:
553-
for val, key in zip(_meta, meta_keys):
553+
for val, key in zip(_meta, meta_keys, strict=True):
554554
if level + 1 == len(val):
555555
seen_meta[key] = _pull_field(obj, val[-1])
556556

@@ -567,7 +567,7 @@ def _recursive_extract(data, path, seen_meta, level: int = 0) -> None:
567567

568568
# For repeating the metadata later
569569
lengths.append(len(recs))
570-
for val, key in zip(_meta, meta_keys):
570+
for val, key in zip(_meta, meta_keys, strict=True):
571571
if level + 1 > len(val):
572572
meta_val = seen_meta[key]
573573
else:

pandas/io/json/_table_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ def build_table_schema(
308308
if index:
309309
if data.index.nlevels > 1:
310310
data.index = cast("MultiIndex", data.index)
311-
for level, name in zip(data.index.levels, data.index.names):
311+
for level, name in zip(data.index.levels, data.index.names, strict=True):
312312
new_field = convert_pandas_type_to_json_field(level)
313313
new_field["name"] = name
314314
fields.append(new_field)

0 commit comments

Comments
 (0)