Skip to content

Commit

Permalink
TST/CLN: Tests parametrizations
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Jan 4, 2024
1 parent b2bca5e commit 1dc983e
Show file tree
Hide file tree
Showing 40 changed files with 246 additions and 328 deletions.
8 changes: 3 additions & 5 deletions pandas/tests/plotting/frame/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,12 +1199,10 @@ def test_hist_df_orientation(self):
axes = df.plot.hist(rot=50, fontsize=8, orientation="horizontal")
_check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8)

@pytest.mark.parametrize(
"weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))]
)
def test_hist_weights(self, weights):
@pytest.mark.parametrize("weight_shape", [(100,), (100, 2)])
def test_hist_weights(self, weight_shape):
# GH 33173

weights = 0.1 * np.ones(shape=weight_shape)
df = DataFrame(
dict(zip(["A", "B"], np.random.default_rng(2).standard_normal((2, 100))))
)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/plotting/frame/test_frame_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ def _check_colors_box(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=Non


class TestDataFrameColor:
@pytest.mark.parametrize(
"color", ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"]
)
@pytest.mark.parametrize("color", list(range(10)))
def test_mpl2_color_cycle_str(self, color):
# GH 15516
color = f"C{color}"
df = DataFrame(
np.random.default_rng(2).standard_normal((10, 3)), columns=["a", "b", "c"]
)
Expand Down
40 changes: 14 additions & 26 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,18 +1144,7 @@ def test_timedelta64_analytics(self):
expected = Timedelta("1 days")
assert result == expected

@pytest.mark.parametrize(
"test_input,error_type",
[
(Series([], dtype="float64"), ValueError),
# For strings, or any Series with dtype 'O'
(Series(["foo", "bar", "baz"]), TypeError),
(Series([(1,), (2,)]), TypeError),
# For mixed data types
(Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"]), TypeError),
],
)
def test_assert_idxminmax_empty_raises(self, test_input, error_type):
def test_assert_idxminmax_empty_raises(self):
"""
Cases where ``Series.argmax`` and related should raise an exception
"""
Expand Down Expand Up @@ -1294,13 +1283,14 @@ def test_minmax_nat_series(self, nat_ser):
@pytest.mark.parametrize(
"nat_df",
[
DataFrame([NaT, NaT]),
DataFrame([NaT, Timedelta("nat")]),
DataFrame([Timedelta("nat"), Timedelta("nat")]),
[NaT, NaT],
[NaT, Timedelta("nat")],
[Timedelta("nat"), Timedelta("nat")],
],
)
def test_minmax_nat_dataframe(self, nat_df):
# GH#23282
nat_df = DataFrame(nat_df)
assert nat_df.min()[0] is NaT
assert nat_df.max()[0] is NaT
assert nat_df.min(skipna=False)[0] is NaT
Expand Down Expand Up @@ -1399,14 +1389,10 @@ class TestSeriesMode:
# were moved from a series-specific test file, _not_ that these tests are
# intended long-term to be series-specific

@pytest.mark.parametrize(
"dropna, expected",
[(True, Series([], dtype=np.float64)), (False, Series([], dtype=np.float64))],
)
def test_mode_empty(self, dropna, expected):
def test_mode_empty(self, dropna):
s = Series([], dtype=np.float64)
result = s.mode(dropna)
tm.assert_series_equal(result, expected)
tm.assert_series_equal(result, s)

@pytest.mark.parametrize(
"dropna, data, expected",
Expand Down Expand Up @@ -1619,23 +1605,24 @@ def test_mode_boolean_with_na(self):
[
(
[0, 1j, 1, 1, 1 + 1j, 1 + 2j],
Series([1], dtype=np.complex128),
[1],
np.complex128,
),
(
[0, 1j, 1, 1, 1 + 1j, 1 + 2j],
Series([1], dtype=np.complex64),
[1],
np.complex64,
),
(
[1 + 1j, 2j, 1 + 1j],
Series([1 + 1j], dtype=np.complex128),
[1 + 1j],
np.complex128,
),
],
)
def test_single_mode_value_complex(self, array, expected, dtype):
result = Series(array, dtype=dtype).mode()
expected = Series(expected, dtype=dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
Expand All @@ -1644,12 +1631,12 @@ def test_single_mode_value_complex(self, array, expected, dtype):
(
# no modes
[0, 1j, 1, 1 + 1j, 1 + 2j],
Series([0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j], dtype=np.complex128),
[0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j],
np.complex128,
),
(
[1 + 1j, 2j, 1 + 1j, 2j, 3],
Series([2j, 1 + 1j], dtype=np.complex64),
[2j, 1 + 1j],
np.complex64,
),
],
Expand All @@ -1659,4 +1646,5 @@ def test_multimode_complex(self, array, expected, dtype):
# mode tries to sort multimodal series.
# Complex numbers are sorted by their magnitude
result = Series(array, dtype=dtype).mode()
expected = Series(expected, dtype=dtype)
tm.assert_series_equal(result, expected)
7 changes: 3 additions & 4 deletions pandas/tests/reshape/concat/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ def test_concat_series_axis1_same_names_ignore_index(self):

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

@pytest.mark.parametrize(
"s1name,s2name", [(np.int64(190), (43, 0)), (190, (43, 0))]
)
def test_concat_series_name_npscalar_tuple(self, s1name, s2name):
@pytest.mark.parametrize("s1name", [np.int64(190), 190])
def test_concat_series_name_npscalar_tuple(self, s1name):
# GH21015
s2name = (43, 0)
s1 = Series({"a": 1, "b": 2}, name=s1name)
s2 = Series({"c": 5, "d": 6}, name=s2name)
result = concat([s1, s2])
Expand Down
24 changes: 10 additions & 14 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,10 +1466,8 @@ def _check_merge(x, y):


class TestMergeDtypes:
@pytest.mark.parametrize(
"right_vals", [["foo", "bar"], Series(["foo", "bar"]).astype("category")]
)
def test_different(self, right_vals):
@pytest.mark.parametrize("dtype", [object, "category"])
def test_different(self, dtype):
left = DataFrame(
{
"A": ["foo", "bar"],
Expand All @@ -1480,6 +1478,7 @@ def test_different(self, right_vals):
"F": Series([1, 2], dtype="int32"),
}
)
right_vals = Series(["foo", "bar"], dtype=dtype)
right = DataFrame({"A": right_vals})

# GH 9780
Expand Down Expand Up @@ -2312,26 +2311,23 @@ def test_merge_suffix(col1, col2, kwargs, expected_cols):
[
(
"right",
DataFrame(
{"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]}
),
{"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]},
),
(
"outer",
DataFrame(
{
"A": [1, 100, 200, 300],
"B1": [80, 60, 70, np.nan],
"B2": [np.nan, 600, 700, 800],
}
),
{
"A": [1, 100, 200, 300],
"B1": [80, 60, 70, np.nan],
"B2": [np.nan, 600, 700, 800],
},
),
],
)
def test_merge_duplicate_suffix(how, expected):
left_df = DataFrame({"A": [100, 200, 1], "B": [60, 70, 80]})
right_df = DataFrame({"A": [100, 200, 300], "B": [600, 700, 800]})
result = merge(left_df, right_df, on="A", how=how, suffixes=("_x", "_x"))
expected = DataFrame(expected)
expected.columns = ["A", "B_x", "B_x"]

tm.assert_frame_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_merge_asof.py
Original file line number Diff line number Diff line change
Expand Up @@ -3099,7 +3099,7 @@ def test_merge_groupby_multiple_column_with_categorical_column(self):
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"func", [lambda x: x, lambda x: to_datetime(x)], ids=["numeric", "datetime"]
"func", [lambda x: x, to_datetime], ids=["numeric", "datetime"]
)
@pytest.mark.parametrize("side", ["left", "right"])
def test_merge_on_nans(self, func, side):
Expand Down
57 changes: 27 additions & 30 deletions pandas/tests/reshape/merge/test_merge_ordered.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,61 +135,58 @@ def test_doc_example(self):
"left, right, on, left_by, right_by, expected",
[
(
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
DataFrame({"T": [2], "E": [1]}),
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
{"T": [2], "E": [1]},
["T"],
["G", "H"],
None,
DataFrame(
{
"G": ["g"] * 3,
"H": ["h"] * 3,
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
}
),
{
"G": ["g"] * 3,
"H": ["h"] * 3,
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
},
),
(
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
DataFrame({"T": [2], "E": [1]}),
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
{"T": [2], "E": [1]},
"T",
["G", "H"],
None,
DataFrame(
{
"G": ["g"] * 3,
"H": ["h"] * 3,
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
}
),
{
"G": ["g"] * 3,
"H": ["h"] * 3,
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
},
),
(
DataFrame({"T": [2], "E": [1]}),
DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}),
{"T": [2], "E": [1]},
{"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]},
["T"],
None,
["G", "H"],
DataFrame(
{
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
"G": ["g"] * 3,
"H": ["h"] * 3,
}
),
{
"T": [1, 2, 3],
"E": [np.nan, 1.0, np.nan],
"G": ["g"] * 3,
"H": ["h"] * 3,
},
),
],
)
def test_list_type_by(self, left, right, on, left_by, right_by, expected):
# GH 35269
left = DataFrame(left)
right = DataFrame(right)
result = merge_ordered(
left=left,
right=right,
on=on,
left_by=left_by,
right_by=right_by,
)
expected = DataFrame(expected)

tm.assert_frame_equal(result, expected)

Expand Down
26 changes: 14 additions & 12 deletions pandas/tests/reshape/test_from_dummies.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,32 +298,32 @@ def test_no_prefix_string_cats_contains_get_dummies_NaN_column():
[
pytest.param(
"c",
DataFrame({"": ["a", "b", "c"]}),
{"": ["a", "b", "c"]},
id="default_category is a str",
),
pytest.param(
1,
DataFrame({"": ["a", "b", 1]}),
{"": ["a", "b", 1]},
id="default_category is a int",
),
pytest.param(
1.25,
DataFrame({"": ["a", "b", 1.25]}),
{"": ["a", "b", 1.25]},
id="default_category is a float",
),
pytest.param(
0,
DataFrame({"": ["a", "b", 0]}),
{"": ["a", "b", 0]},
id="default_category is a 0",
),
pytest.param(
False,
DataFrame({"": ["a", "b", False]}),
{"": ["a", "b", False]},
id="default_category is a bool",
),
pytest.param(
(1, 2),
DataFrame({"": ["a", "b", (1, 2)]}),
{"": ["a", "b", (1, 2)]},
id="default_category is a tuple",
),
],
Expand All @@ -333,6 +333,7 @@ def test_no_prefix_string_cats_default_category(
):
dummies = DataFrame({"a": [1, 0, 0], "b": [0, 1, 0]})
result = from_dummies(dummies, default_category=default_category)
expected = DataFrame(expected)
if using_infer_string:
expected[""] = expected[""].astype("string[pyarrow_numpy]")
tm.assert_frame_equal(result, expected)
Expand Down Expand Up @@ -366,32 +367,32 @@ def test_with_prefix_contains_get_dummies_NaN_column():
[
pytest.param(
"x",
DataFrame({"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]}),
{"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]},
id="default_category is a str",
),
pytest.param(
0,
DataFrame({"col1": ["a", "b", 0], "col2": [0, "a", "c"]}),
{"col1": ["a", "b", 0], "col2": [0, "a", "c"]},
id="default_category is a 0",
),
pytest.param(
False,
DataFrame({"col1": ["a", "b", False], "col2": [False, "a", "c"]}),
{"col1": ["a", "b", False], "col2": [False, "a", "c"]},
id="default_category is a False",
),
pytest.param(
{"col2": 1, "col1": 2.5},
DataFrame({"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]}),
{"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]},
id="default_category is a dict with int and float values",
),
pytest.param(
{"col2": None, "col1": False},
DataFrame({"col1": ["a", "b", False], "col2": [None, "a", "c"]}),
{"col1": ["a", "b", False], "col2": [None, "a", "c"]},
id="default_category is a dict with bool and None values",
),
pytest.param(
{"col2": (1, 2), "col1": [1.25, False]},
DataFrame({"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]}),
{"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]},
id="default_category is a dict with list and tuple values",
),
],
Expand All @@ -402,6 +403,7 @@ def test_with_prefix_default_category(
result = from_dummies(
dummies_with_unassigned, sep="_", default_category=default_category
)
expected = DataFrame(expected)
tm.assert_frame_equal(result, expected)


Expand Down

0 comments on commit 1dc983e

Please sign in to comment.