From b7363be50e965c4b0623ee7589da25d9ad590e95 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Mon, 29 Mar 2021 20:28:40 -0400 Subject: [PATCH] CLN/TST: Some cleanups in apply.test_invalid_arg --- pandas/tests/apply/test_invalid_arg.py | 115 +++++++++++-------------- 1 file changed, 48 insertions(+), 67 deletions(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index c3cb3b245affd..698f85a04a757 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -102,26 +102,6 @@ def test_series_nested_renamer(renamer): s.agg(renamer) -def test_agg_dict_nested_renaming_depr_agg(): - - df = DataFrame({"A": range(5), "B": 5}) - - # nested renaming - msg = r"nested renamer is not supported" - with pytest.raises(SpecificationError, match=msg): - df.agg({"A": {"foo": "min"}, "B": {"bar": "max"}}) - - -def test_agg_dict_nested_renaming_depr_transform(): - df = DataFrame({"A": range(5), "B": 5}) - - # nested renaming - msg = r"nested renamer is not supported" - with pytest.raises(SpecificationError, match=msg): - # mypy identifies the argument as an invalid type - df.transform({"A": {"foo": "min"}, "B": {"bar": "max"}}) - - def test_apply_dict_depr(): tsdf = DataFrame( @@ -134,6 +114,17 @@ def test_apply_dict_depr(): tsdf.A.agg({"foo": ["sum", "mean"]}) +@pytest.mark.parametrize("method", ["agg", "transform"]) +def test_dict_nested_renaming_depr(method): + + df = DataFrame({"A": range(5), "B": 5}) + + # nested renaming + msg = r"nested renamer is not supported" + with pytest.raises(SpecificationError, match=msg): + getattr(df, method)({"A": {"foo": "min"}, "B": {"bar": "max"}}) + + @pytest.mark.parametrize("method", ["apply", "agg", "transform"]) @pytest.mark.parametrize("func", [{"B": "sum"}, {"B": ["sum"]}]) def test_missing_column(method, func): @@ -288,25 +279,21 @@ def test_transform_none_to_type(): df.transform({"a": int}) -def test_apply_broadcast_error(int_frame_const_col): +@pytest.mark.parametrize( + "func", + [ + lambda x: np.array([1, 2]).reshape(-1, 2), + lambda x: [1, 2], + lambda x: Series([1, 2]), + ], +) +def test_apply_broadcast_error(int_frame_const_col, func): df = int_frame_const_col # > 1 ndim - msg = "too many dims to broadcast" + msg = "too many dims to broadcast|cannot broadcast result" with pytest.raises(ValueError, match=msg): - df.apply( - lambda x: np.array([1, 2]).reshape(-1, 2), - axis=1, - result_type="broadcast", - ) - - # cannot broadcast - msg = "cannot broadcast result" - with pytest.raises(ValueError, match=msg): - df.apply(lambda x: [1, 2], axis=1, result_type="broadcast") - - with pytest.raises(ValueError, match=msg): - df.apply(lambda x: Series([1, 2]), axis=1, result_type="broadcast") + df.apply(func, axis=1, result_type="broadcast") def test_transform_and_agg_err_agg(axis, float_frame): @@ -317,34 +304,47 @@ def test_transform_and_agg_err_agg(axis, float_frame): float_frame.agg(["max", "sqrt"], axis=axis) -def test_transform_and_agg_err_series(string_series): +@pytest.mark.parametrize( + "func, msg", + [ + (["sqrt", "max"], "cannot combine transform and aggregation"), + ( + {"foo": np.sqrt, "bar": "sum"}, + "cannot perform both aggregation and transformation", + ), + ], +) +def test_transform_and_agg_err_series(string_series, func, msg): # we are trying to transform with an aggregator - msg = "cannot combine transform and aggregation" with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): - string_series.agg(["sqrt", "max"]) + string_series.agg(func) - msg = "cannot perform both aggregation and transformation" - with pytest.raises(ValueError, match=msg): - with np.errstate(all="ignore"): - string_series.agg({"foo": np.sqrt, "bar": "sum"}) - -def test_transform_and_agg_err_frame(axis, float_frame): +@pytest.mark.parametrize("func", [["max", "min"], ["max", "sqrt"]]) +def test_transform_wont_agg_frame(axis, float_frame, func): # GH 35964 # cannot both transform and agg msg = "Function did not transform" with pytest.raises(ValueError, match=msg): - float_frame.transform(["max", "min"], axis=axis) + float_frame.transform(func, axis=axis) + +@pytest.mark.parametrize("func", [["min", "max"], ["sqrt", "max"]]) +def test_transform_wont_agg_series(string_series, func): + # GH 35964 + # we are trying to transform with an aggregator msg = "Function did not transform" with pytest.raises(ValueError, match=msg): - float_frame.transform(["max", "sqrt"], axis=axis) + string_series.transform(func) -def test_transform_reducer_raises(all_reductions, frame_or_series): +@pytest.mark.parametrize( + "op_wrapper", [lambda x: x, lambda x: [x], lambda x: {"A": x}, lambda x: {"A": [x]}] +) +def test_transform_reducer_raises(all_reductions, frame_or_series, op_wrapper): # GH 35964 - op = all_reductions + op = op_wrapper(all_reductions) obj = DataFrame({"A": [1, 2, 3]}) if frame_or_series is not DataFrame: @@ -353,22 +353,3 @@ def test_transform_reducer_raises(all_reductions, frame_or_series): msg = "Function did not transform" with pytest.raises(ValueError, match=msg): obj.transform(op) - with pytest.raises(ValueError, match=msg): - obj.transform([op]) - with pytest.raises(ValueError, match=msg): - obj.transform({"A": op}) - with pytest.raises(ValueError, match=msg): - obj.transform({"A": [op]}) - - -def test_transform_wont_agg(string_series): - # GH 35964 - # we are trying to transform with an aggregator - msg = "Function did not transform" - with pytest.raises(ValueError, match=msg): - string_series.transform(["min", "max"]) - - msg = "Function did not transform" - with pytest.raises(ValueError, match=msg): - with np.errstate(all="ignore"): - string_series.transform(["sqrt", "max"])