Skip to content

Commit 6912d50

Browse files
authored
TST/CLN: Tests parametrizations 3 (#56745)
* TST/CLN: Tests parametrizations * Fix typos * fix typos * Add pytest
1 parent 3b13e8a commit 6912d50

40 files changed

+248
-326
lines changed

pandas/tests/plotting/frame/test_frame.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,12 +1199,10 @@ def test_hist_df_orientation(self):
11991199
axes = df.plot.hist(rot=50, fontsize=8, orientation="horizontal")
12001200
_check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8)
12011201

1202-
@pytest.mark.parametrize(
1203-
"weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))]
1204-
)
1205-
def test_hist_weights(self, weights):
1202+
@pytest.mark.parametrize("weight_shape", [(100,), (100, 2)])
1203+
def test_hist_weights(self, weight_shape):
12061204
# GH 33173
1207-
1205+
weights = 0.1 * np.ones(shape=weight_shape)
12081206
df = DataFrame(
12091207
dict(zip(["A", "B"], np.random.default_rng(2).standard_normal((2, 100))))
12101208
)

pandas/tests/plotting/frame/test_frame_color.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ def _check_colors_box(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=Non
3030

3131

3232
class TestDataFrameColor:
33-
@pytest.mark.parametrize(
34-
"color", ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
35-
)
33+
@pytest.mark.parametrize("color", list(range(10)))
3634
def test_mpl2_color_cycle_str(self, color):
3735
# GH 15516
36+
color = f"C{color}"
3837
df = DataFrame(
3938
np.random.default_rng(2).standard_normal((10, 3)), columns=["a", "b", "c"]
4039
)

pandas/tests/reductions/test_reductions.py

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,18 +1144,7 @@ def test_timedelta64_analytics(self):
11441144
expected = Timedelta("1 days")
11451145
assert result == expected
11461146

1147-
@pytest.mark.parametrize(
1148-
"test_input,error_type",
1149-
[
1150-
(Series([], dtype="float64"), ValueError),
1151-
# For strings, or any Series with dtype 'O'
1152-
(Series(["foo", "bar", "baz"]), TypeError),
1153-
(Series([(1,), (2,)]), TypeError),
1154-
# For mixed data types
1155-
(Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"]), TypeError),
1156-
],
1157-
)
1158-
def test_assert_idxminmax_empty_raises(self, test_input, error_type):
1147+
def test_assert_idxminmax_empty_raises(self):
11591148
"""
11601149
Cases where ``Series.argmax`` and related should raise an exception
11611150
"""
@@ -1294,13 +1283,14 @@ def test_minmax_nat_series(self, nat_ser):
12941283
@pytest.mark.parametrize(
12951284
"nat_df",
12961285
[
1297-
DataFrame([NaT, NaT]),
1298-
DataFrame([NaT, Timedelta("nat")]),
1299-
DataFrame([Timedelta("nat"), Timedelta("nat")]),
1286+
[NaT, NaT],
1287+
[NaT, Timedelta("nat")],
1288+
[Timedelta("nat"), Timedelta("nat")],
13001289
],
13011290
)
13021291
def test_minmax_nat_dataframe(self, nat_df):
13031292
# GH#23282
1293+
nat_df = DataFrame(nat_df)
13041294
assert nat_df.min()[0] is NaT
13051295
assert nat_df.max()[0] is NaT
13061296
assert nat_df.min(skipna=False)[0] is NaT
@@ -1399,14 +1389,10 @@ class TestSeriesMode:
13991389
# were moved from a series-specific test file, _not_ that these tests are
14001390
# intended long-term to be series-specific
14011391

1402-
@pytest.mark.parametrize(
1403-
"dropna, expected",
1404-
[(True, Series([], dtype=np.float64)), (False, Series([], dtype=np.float64))],
1405-
)
1406-
def test_mode_empty(self, dropna, expected):
1392+
def test_mode_empty(self, dropna):
14071393
s = Series([], dtype=np.float64)
14081394
result = s.mode(dropna)
1409-
tm.assert_series_equal(result, expected)
1395+
tm.assert_series_equal(result, s)
14101396

14111397
@pytest.mark.parametrize(
14121398
"dropna, data, expected",
@@ -1619,23 +1605,24 @@ def test_mode_boolean_with_na(self):
16191605
[
16201606
(
16211607
[0, 1j, 1, 1, 1 + 1j, 1 + 2j],
1622-
Series([1], dtype=np.complex128),
1608+
[1],
16231609
np.complex128,
16241610
),
16251611
(
16261612
[0, 1j, 1, 1, 1 + 1j, 1 + 2j],
1627-
Series([1], dtype=np.complex64),
1613+
[1],
16281614
np.complex64,
16291615
),
16301616
(
16311617
[1 + 1j, 2j, 1 + 1j],
1632-
Series([1 + 1j], dtype=np.complex128),
1618+
[1 + 1j],
16331619
np.complex128,
16341620
),
16351621
],
16361622
)
16371623
def test_single_mode_value_complex(self, array, expected, dtype):
16381624
result = Series(array, dtype=dtype).mode()
1625+
expected = Series(expected, dtype=dtype)
16391626
tm.assert_series_equal(result, expected)
16401627

16411628
@pytest.mark.parametrize(
@@ -1644,12 +1631,12 @@ def test_single_mode_value_complex(self, array, expected, dtype):
16441631
(
16451632
# no modes
16461633
[0, 1j, 1, 1 + 1j, 1 + 2j],
1647-
Series([0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j], dtype=np.complex128),
1634+
[0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j],
16481635
np.complex128,
16491636
),
16501637
(
16511638
[1 + 1j, 2j, 1 + 1j, 2j, 3],
1652-
Series([2j, 1 + 1j], dtype=np.complex64),
1639+
[2j, 1 + 1j],
16531640
np.complex64,
16541641
),
16551642
],
@@ -1659,4 +1646,5 @@ def test_multimode_complex(self, array, expected, dtype):
16591646
# mode tries to sort multimodal series.
16601647
# Complex numbers are sorted by their magnitude
16611648
result = Series(array, dtype=dtype).mode()
1649+
expected = Series(expected, dtype=dtype)
16621650
tm.assert_series_equal(result, expected)

pandas/tests/reshape/concat/test_series.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ def test_concat_series_axis1_same_names_ignore_index(self):
128128

129129
tm.assert_index_equal(result.columns, expected, exact=True)
130130

131-
@pytest.mark.parametrize(
132-
"s1name,s2name", [(np.int64(190), (43, 0)), (190, (43, 0))]
133-
)
134-
def test_concat_series_name_npscalar_tuple(self, s1name, s2name):
131+
@pytest.mark.parametrize("s1name", [np.int64(190), 190])
132+
def test_concat_series_name_npscalar_tuple(self, s1name):
135133
# GH21015
134+
s2name = (43, 0)
136135
s1 = Series({"a": 1, "b": 2}, name=s1name)
137136
s2 = Series({"c": 5, "d": 6}, name=s2name)
138137
result = concat([s1, s2])

pandas/tests/reshape/merge/test_merge.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,10 +1466,8 @@ def _check_merge(x, y):
14661466

14671467

14681468
class TestMergeDtypes:
1469-
@pytest.mark.parametrize(
1470-
"right_vals", [["foo", "bar"], Series(["foo", "bar"]).astype("category")]
1471-
)
1472-
def test_different(self, right_vals):
1469+
@pytest.mark.parametrize("dtype", [object, "category"])
1470+
def test_different(self, dtype):
14731471
left = DataFrame(
14741472
{
14751473
"A": ["foo", "bar"],
@@ -1480,6 +1478,7 @@ def test_different(self, right_vals):
14801478
"F": Series([1, 2], dtype="int32"),
14811479
}
14821480
)
1481+
right_vals = Series(["foo", "bar"], dtype=dtype)
14831482
right = DataFrame({"A": right_vals})
14841483

14851484
# GH 9780
@@ -2311,26 +2310,23 @@ def test_merge_suffix(col1, col2, kwargs, expected_cols):
23112310
[
23122311
(
23132312
"right",
2314-
DataFrame(
2315-
{"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]}
2316-
),
2313+
{"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]},
23172314
),
23182315
(
23192316
"outer",
2320-
DataFrame(
2321-
{
2322-
"A": [1, 100, 200, 300],
2323-
"B1": [80, 60, 70, np.nan],
2324-
"B2": [np.nan, 600, 700, 800],
2325-
}
2326-
),
2317+
{
2318+
"A": [1, 100, 200, 300],
2319+
"B1": [80, 60, 70, np.nan],
2320+
"B2": [np.nan, 600, 700, 800],
2321+
},
23272322
),
23282323
],
23292324
)
23302325
def test_merge_duplicate_suffix(how, expected):
23312326
left_df = DataFrame({"A": [100, 200, 1], "B": [60, 70, 80]})
23322327
right_df = DataFrame({"A": [100, 200, 300], "B": [600, 700, 800]})
23332328
result = merge(left_df, right_df, on="A", how=how, suffixes=("_x", "_x"))
2329+
expected = DataFrame(expected)
23342330
expected.columns = ["A", "B_x", "B_x"]
23352331

23362332
tm.assert_frame_equal(result, expected)

pandas/tests/reshape/merge/test_merge_asof.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3099,7 +3099,7 @@ def test_merge_groupby_multiple_column_with_categorical_column(self):
30993099
tm.assert_frame_equal(result, expected)
31003100

31013101
@pytest.mark.parametrize(
3102-
"func", [lambda x: x, lambda x: to_datetime(x)], ids=["numeric", "datetime"]
3102+
"func", [lambda x: x, to_datetime], ids=["numeric", "datetime"]
31033103
)
31043104
@pytest.mark.parametrize("side", ["left", "right"])
31053105
def test_merge_on_nans(self, func, side):

pandas/tests/reshape/merge/test_merge_ordered.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,61 +135,58 @@ def test_doc_example(self):
135135
"left, right, on, left_by, right_by, expected",
136136
[
137137
(
138-
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
139-
DataFrame({"T": [2], "E": [1]}),
138+
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
139+
{"T": [2], "E": [1]},
140140
["T"],
141141
["G", "H"],
142142
None,
143-
DataFrame(
144-
{
145-
"G": ["g"] * 3,
146-
"H": ["h"] * 3,
147-
"T": [1, 2, 3],
148-
"E": [np.nan, 1.0, np.nan],
149-
}
150-
),
143+
{
144+
"G": ["g"] * 3,
145+
"H": ["h"] * 3,
146+
"T": [1, 2, 3],
147+
"E": [np.nan, 1.0, np.nan],
148+
},
151149
),
152150
(
153-
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
154-
DataFrame({"T": [2], "E": [1]}),
151+
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
152+
{"T": [2], "E": [1]},
155153
"T",
156154
["G", "H"],
157155
None,
158-
DataFrame(
159-
{
160-
"G": ["g"] * 3,
161-
"H": ["h"] * 3,
162-
"T": [1, 2, 3],
163-
"E": [np.nan, 1.0, np.nan],
164-
}
165-
),
156+
{
157+
"G": ["g"] * 3,
158+
"H": ["h"] * 3,
159+
"T": [1, 2, 3],
160+
"E": [np.nan, 1.0, np.nan],
161+
},
166162
),
167163
(
168-
DataFrame({"T": [2], "E": [1]}),
169-
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
164+
{"T": [2], "E": [1]},
165+
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
170166
["T"],
171167
None,
172168
["G", "H"],
173-
DataFrame(
174-
{
175-
"T": [1, 2, 3],
176-
"E": [np.nan, 1.0, np.nan],
177-
"G": ["g"] * 3,
178-
"H": ["h"] * 3,
179-
}
180-
),
169+
{
170+
"T": [1, 2, 3],
171+
"E": [np.nan, 1.0, np.nan],
172+
"G": ["g"] * 3,
173+
"H": ["h"] * 3,
174+
},
181175
),
182176
],
183177
)
184178
def test_list_type_by(self, left, right, on, left_by, right_by, expected):
185179
# GH 35269
180+
left = DataFrame(left)
181+
right = DataFrame(right)
186182
result = merge_ordered(
187183
left=left,
188184
right=right,
189185
on=on,
190186
left_by=left_by,
191187
right_by=right_by,
192188
)
189+
expected = DataFrame(expected)
193190

194191
tm.assert_frame_equal(result, expected)
195192

pandas/tests/reshape/test_from_dummies.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,32 +298,32 @@ def test_no_prefix_string_cats_contains_get_dummies_NaN_column():
298298
[
299299
pytest.param(
300300
"c",
301-
DataFrame({"": ["a", "b", "c"]}),
301+
{"": ["a", "b", "c"]},
302302
id="default_category is a str",
303303
),
304304
pytest.param(
305305
1,
306-
DataFrame({"": ["a", "b", 1]}),
306+
{"": ["a", "b", 1]},
307307
id="default_category is a int",
308308
),
309309
pytest.param(
310310
1.25,
311-
DataFrame({"": ["a", "b", 1.25]}),
311+
{"": ["a", "b", 1.25]},
312312
id="default_category is a float",
313313
),
314314
pytest.param(
315315
0,
316-
DataFrame({"": ["a", "b", 0]}),
316+
{"": ["a", "b", 0]},
317317
id="default_category is a 0",
318318
),
319319
pytest.param(
320320
False,
321-
DataFrame({"": ["a", "b", False]}),
321+
{"": ["a", "b", False]},
322322
id="default_category is a bool",
323323
),
324324
pytest.param(
325325
(1, 2),
326-
DataFrame({"": ["a", "b", (1, 2)]}),
326+
{"": ["a", "b", (1, 2)]},
327327
id="default_category is a tuple",
328328
),
329329
],
@@ -333,6 +333,7 @@ def test_no_prefix_string_cats_default_category(
333333
):
334334
dummies = DataFrame({"a": [1, 0, 0], "b": [0, 1, 0]})
335335
result = from_dummies(dummies, default_category=default_category)
336+
expected = DataFrame(expected)
336337
if using_infer_string:
337338
expected[""] = expected[""].astype("string[pyarrow_numpy]")
338339
tm.assert_frame_equal(result, expected)
@@ -366,32 +367,32 @@ def test_with_prefix_contains_get_dummies_NaN_column():
366367
[
367368
pytest.param(
368369
"x",
369-
DataFrame({"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]}),
370+
{"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]},
370371
id="default_category is a str",
371372
),
372373
pytest.param(
373374
0,
374-
DataFrame({"col1": ["a", "b", 0], "col2": [0, "a", "c"]}),
375+
{"col1": ["a", "b", 0], "col2": [0, "a", "c"]},
375376
id="default_category is a 0",
376377
),
377378
pytest.param(
378379
False,
379-
DataFrame({"col1": ["a", "b", False], "col2": [False, "a", "c"]}),
380+
{"col1": ["a", "b", False], "col2": [False, "a", "c"]},
380381
id="default_category is a False",
381382
),
382383
pytest.param(
383384
{"col2": 1, "col1": 2.5},
384-
DataFrame({"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]}),
385+
{"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]},
385386
id="default_category is a dict with int and float values",
386387
),
387388
pytest.param(
388389
{"col2": None, "col1": False},
389-
DataFrame({"col1": ["a", "b", False], "col2": [None, "a", "c"]}),
390+
{"col1": ["a", "b", False], "col2": [None, "a", "c"]},
390391
id="default_category is a dict with bool and None values",
391392
),
392393
pytest.param(
393394
{"col2": (1, 2), "col1": [1.25, False]},
394-
DataFrame({"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]}),
395+
{"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]},
395396
id="default_category is a dict with list and tuple values",
396397
),
397398
],
@@ -402,6 +403,7 @@ def test_with_prefix_default_category(
402403
result = from_dummies(
403404
dummies_with_unassigned, sep="_", default_category=default_category
404405
)
406+
expected = DataFrame(expected)
405407
tm.assert_frame_equal(result, expected)
406408

407409

0 commit comments

Comments
 (0)