Skip to content

Commit 842a2c6

Browse files
authored
STY: Add strict=True in zip() in \core, \tests and \scripts (#62984)
1 parent 9f66b81 commit 842a2c6

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

pandas/core/reshape/merge.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ def _maybe_restore_index_levels(self, result: DataFrame) -> None:
12481248
"""
12491249
names_to_restore = []
12501250
for name, left_key, right_key in zip(
1251-
self.join_names, self.left_on, self.right_on
1251+
self.join_names, self.left_on, self.right_on, strict=True
12521252
):
12531253
if (
12541254
# Argument 1 to "_is_level_reference" of "NDFrame" has incompatible
@@ -1281,7 +1281,7 @@ def _maybe_add_join_keys(
12811281

12821282
assert all(isinstance(x, _known) for x in self.left_join_keys)
12831283

1284-
keys = zip(self.join_names, self.left_on, self.right_on)
1284+
keys = zip(self.join_names, self.left_on, self.right_on, strict=True)
12851285
for i, (name, lname, rname) in enumerate(keys):
12861286
if not _should_fill(lname, rname):
12871287
continue
@@ -1590,7 +1590,7 @@ def _get_merge_keys(
15901590

15911591
# ugh, spaghetti re #733
15921592
if _any(self.left_on) and _any(self.right_on):
1593-
for lk, rk in zip(self.left_on, self.right_on):
1593+
for lk, rk in zip(self.left_on, self.right_on, strict=True):
15941594
lk = extract_array(lk, extract_numpy=True)
15951595
rk = extract_array(rk, extract_numpy=True)
15961596
if is_lkey(lk):
@@ -1653,7 +1653,7 @@ def _get_merge_keys(
16531653
right_keys = [
16541654
lev._values.take(lev_codes)
16551655
for lev, lev_codes in zip(
1656-
self.right.index.levels, self.right.index.codes
1656+
self.right.index.levels, self.right.index.codes, strict=True
16571657
)
16581658
]
16591659
else:
@@ -1675,7 +1675,7 @@ def _get_merge_keys(
16751675
left_keys = [
16761676
lev._values.take(lev_codes)
16771677
for lev, lev_codes in zip(
1678-
self.left.index.levels, self.left.index.codes
1678+
self.left.index.levels, self.left.index.codes, strict=True
16791679
)
16801680
]
16811681
else:
@@ -1692,7 +1692,7 @@ def _maybe_coerce_merge_keys(self) -> None:
16921692
# or if we have object and integer dtypes
16931693

16941694
for lk, rk, name in zip(
1695-
self.left_join_keys, self.right_join_keys, self.join_names
1695+
self.left_join_keys, self.right_join_keys, self.join_names, strict=True
16961696
):
16971697
if (len(lk) and not len(rk)) or (not len(lk) and len(rk)):
16981698
continue
@@ -2084,7 +2084,7 @@ def get_join_indexers(
20842084
_factorize_keys(left_keys[n], right_keys[n], sort=sort)
20852085
for n in range(len(left_keys))
20862086
)
2087-
zipped = zip(*mapped)
2087+
zipped = zip(*mapped, strict=True)
20882088
llab, rlab, shape = (list(x) for x in zipped)
20892089

20902090
# get flat i8 keys from label lists
@@ -2469,7 +2469,7 @@ def _check_dtype_match(left: ArrayLike, right: ArrayLike, i: int) -> None:
24692469
raise MergeError(msg)
24702470

24712471
# validate index types are the same
2472-
for i, (lk, rk) in enumerate(zip(left_join_keys, right_join_keys)):
2472+
for i, (lk, rk) in enumerate(zip(left_join_keys, right_join_keys, strict=True)):
24732473
_check_dtype_match(lk, rk, i)
24742474

24752475
if self.left_index:
@@ -2654,7 +2654,7 @@ def _get_multiindex_indexer(
26542654
_factorize_keys(index.levels[n]._values, join_keys[n], sort=sort)
26552655
for n in range(index.nlevels)
26562656
)
2657-
zipped = zip(*mapped)
2657+
zipped = zip(*mapped, strict=True)
26582658
rcodes, lcodes, shape = (list(x) for x in zipped)
26592659
if sort:
26602660
rcodes = list(map(np.take, rcodes, index.codes))

pandas/tests/plotting/frame/test_frame.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def test_plot_title(self):
183183

184184
@pytest.mark.slow
185185
def test_plot_multiindex(self):
186-
tuples = zip(string.ascii_letters[:10], range(10))
186+
tuples = zip(string.ascii_letters[:10], range(10), strict=True)
187187
df = DataFrame(
188188
np.random.default_rng(2).random((10, 3)),
189189
index=MultiIndex.from_tuples(tuples),
@@ -513,7 +513,7 @@ def test_negative_log(self):
513513

514514
def _compare_stacked_y_cood(self, normal_lines, stacked_lines):
515515
base = np.zeros(len(normal_lines[0].get_data()[1]))
516-
for nl, sl in zip(normal_lines, stacked_lines):
516+
for nl, sl in zip(normal_lines, stacked_lines, strict=True):
517517
base += nl.get_data()[1] # get y coordinates
518518
sy = sl.get_data()[1]
519519
tm.assert_numpy_array_equal(base, sy)
@@ -920,7 +920,10 @@ def test_scatterplot_color_by_categorical(self, ordered, categories):
920920

921921
expected_yticklabels = categories
922922
result_yticklabels = [i.get_text() for i in colorbar.ax.get_ymajorticklabels()]
923-
assert all(i == j for i, j in zip(result_yticklabels, expected_yticklabels))
923+
assert all(
924+
i == j
925+
for i, j in zip(result_yticklabels, expected_yticklabels, strict=True)
926+
)
924927

925928
@pytest.mark.parametrize("x, y", [("x", "y"), ("y", "x"), ("y", "y")])
926929
def test_plot_scatter_with_categorical_data(self, x, y):
@@ -1131,7 +1134,7 @@ def test_boxplot_vertical_subplots(self, hist_df):
11311134
)
11321135
_check_axes_shape(axes, axes_num=3, layout=(1, 3))
11331136
_check_ax_scales(axes, xaxis="log")
1134-
for ax, label in zip(axes, labels):
1137+
for ax, label in zip(axes, labels, strict=True):
11351138
_check_text_labels(ax.get_yticklabels(), [label])
11361139
assert len(ax.lines) == 7
11371140

@@ -1258,7 +1261,13 @@ def test_hist_weights(self, weight_shape):
12581261
# GH 33173
12591262
weights = 0.1 * np.ones(shape=weight_shape)
12601263
df = DataFrame(
1261-
dict(zip(["A", "B"], np.random.default_rng(2).standard_normal((2, 100))))
1264+
dict(
1265+
zip(
1266+
["A", "B"],
1267+
np.random.default_rng(2).standard_normal((2, 100)),
1268+
strict=True,
1269+
)
1270+
)
12621271
)
12631272

12641273
ax1 = _check_plot_works(df.plot, kind="hist", weights=weights)
@@ -1679,7 +1688,7 @@ def test_pie_df_subplots(self):
16791688
assert len(axes) == len(df.columns)
16801689
for ax in axes:
16811690
_check_text_labels(ax.texts, df.index)
1682-
for ax, ylabel in zip(axes, df.columns):
1691+
for ax, ylabel in zip(axes, df.columns, strict=True):
16831692
assert ax.get_ylabel() == ""
16841693

16851694
def test_pie_df_labels_colors(self):
@@ -2381,7 +2390,7 @@ def test_x_string_values_ticks(self):
23812390
ax = df.plot.area(x="day")
23822391
ax.set_xlim(-1, 3)
23832392
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
2384-
labels_position = dict(zip(xticklabels, ax.get_xticks()))
2393+
labels_position = dict(zip(xticklabels, ax.get_xticks(), strict=False))
23852394
# Testing if the label stayed at the right position
23862395
assert labels_position["Monday"] == 0.0
23872396
assert labels_position["Tuesday"] == 1.0
@@ -2399,7 +2408,7 @@ def test_x_multiindex_values_ticks(self):
23992408
ax = df.plot()
24002409
ax.set_xlim(-1, 4)
24012410
xticklabels = [t.get_text() for t in ax.get_xticklabels()]
2402-
labels_position = dict(zip(xticklabels, ax.get_xticks()))
2411+
labels_position = dict(zip(xticklabels, ax.get_xticks(), strict=False))
24032412
# Testing if the label stayed at the right position
24042413
assert labels_position["(2012, 1)"] == 0.0
24052414
assert labels_position["(2012, 2)"] == 1.0
@@ -2475,7 +2484,7 @@ def test_group_subplot(self, kind):
24752484
assert len(axes) == 3 # 2 groups + single column a
24762485

24772486
expected_labels = (["b", "e"], ["c", "d"], ["a"])
2478-
for ax, labels in zip(axes, expected_labels):
2487+
for ax, labels in zip(axes, expected_labels, strict=True):
24792488
if kind != "pie":
24802489
_check_legend_labels(ax, labels=labels)
24812490
if kind == "line":

pandas/tests/reshape/merge/test_merge_asof.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,14 +3249,14 @@ def test_timedelta_tolerance_nearest(self, unit):
32493249
)
32503250

32513251
left = pd.DataFrame(
3252-
list(zip([0, 5, 10, 15, 20, 25], [0, 1, 2, 3, 4, 5])),
3252+
list(zip([0, 5, 10, 15, 20, 25], [0, 1, 2, 3, 4, 5], strict=True)),
32533253
columns=["time", "left"],
32543254
)
32553255

32563256
left["time"] = pd.to_timedelta(left["time"], "ms").astype(f"m8[{unit}]")
32573257

32583258
right = pd.DataFrame(
3259-
list(zip([0, 3, 9, 12, 15, 18], [0, 1, 2, 3, 4, 5])),
3259+
list(zip([0, 3, 9, 12, 15, 18], [0, 1, 2, 3, 4, 5], strict=True)),
32603260
columns=["time", "right"],
32613261
)
32623262

@@ -3268,6 +3268,7 @@ def test_timedelta_tolerance_nearest(self, unit):
32683268
[0, 5, 10, 15, 20, 25],
32693269
[0, 1, 2, 3, 4, 5],
32703270
[0, np.nan, 2, 4, np.nan, np.nan],
3271+
strict=True,
32713272
)
32723273
),
32733274
columns=["time", "left", "right"],

scripts/validate_unwanted_patterns.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ def has_wrong_whitespace(first_line: str, second_line: str) -> bool:
266266

267267
tokens: list = list(tokenize.generate_tokens(file_obj.readline))
268268

269-
for first_token, second_token, third_token in zip(tokens, tokens[1:], tokens[2:]):
269+
for first_token, second_token, third_token in zip(
270+
tokens, tokens[1:], tokens[2:], strict=False
271+
):
270272
# Checking if we are in a block of concated string
271273
if (
272274
first_token.type == third_token.type == token.STRING

0 commit comments

Comments
 (0)