From d97567836c022abe30478b41e52a9bddb858763f Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Sat, 21 May 2022 15:36:39 -0400 Subject: [PATCH] Backport PR #47078: REGR: Raise NotImplementedError for agg with axis=1 and multiple funcs --- doc/source/whatsnew/v1.4.3.rst | 2 ++ pandas/core/apply.py | 6 ++++++ pandas/tests/groupby/aggregate/test_aggregate.py | 11 +++++++++++ pandas/tests/resample/test_resample_api.py | 14 ++++++++++++++ pandas/tests/window/test_api.py | 11 +++++++++++ 5 files changed, 44 insertions(+) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index 7c09eec212d69..415a3ff4efda0 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -18,6 +18,8 @@ Fixed regressions - Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`) - Fixed regression in :meth:`.Groupby.transform` and :meth:`.Groupby.agg` failing with ``engine="numba"`` when the index was a :class:`MultiIndex` (:issue:`46867`) - Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`) +- Fixed regression in :meth:`.DataFrameGroupBy.agg` when used with list-likes or dict-likes and ``axis=1`` that would give incorrect results; now raises ``NotImplementedError`` (:issue:`46995`) +- Fixed regression in :meth:`DataFrame.resample` and :meth:`DataFrame.rolling` when used with list-likes or dict-likes and ``axis=1`` that would raise an unintuitive error message; now raises ``NotImplementedError`` (:issue:`46904`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 64ee843f1d946..53e1b01d2a3d0 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -325,6 +325,9 @@ def agg_list_like(self) -> DataFrame | Series: obj = self.obj arg = cast(List[AggFuncTypeBase], self.f) + if getattr(obj, "axis", 0) == 1: + raise NotImplementedError("axis other than 0 is not supported") + if not isinstance(obj, SelectionMixin): # i.e. obj is Series or DataFrame selected_obj = obj @@ -456,6 +459,9 @@ def agg_dict_like(self) -> DataFrame | Series: obj = self.obj arg = cast(AggFuncTypeDict, self.f) + if getattr(obj, "axis", 0) == 1: + raise NotImplementedError("axis other than 0 is not supported") + if not isinstance(obj, SelectionMixin): # i.e. obj is Series or DataFrame selected_obj = obj diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 1ea44871eea4d..15ad81e3ffba8 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1393,3 +1393,14 @@ def test_groupby_complex_raises(func): msg = "No matching signature found" with pytest.raises(TypeError, match=msg): data.groupby(data.index % 2).agg(func) + + +@pytest.mark.parametrize( + "func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}] +) +def test_multi_axis_1_raises(func): + # GH#46995 + df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]}) + gb = df.groupby("a", axis=1) + with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): + gb.agg(func) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index bb49450b8414e..c052870fe0bfd 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -513,6 +513,20 @@ def test_agg_misc(): t[["A"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]}) +@pytest.mark.parametrize( + "func", [["min"], ["mean", "max"], {"A": "sum"}, {"A": "prod", "B": "median"}] +) +def test_multi_agg_axis_1_raises(func): + # GH#46904 + np.random.seed(1234) + index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D") + index.name = "date" + df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T + res = df.resample("M", axis=1) + with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): + res.agg(func) + + def test_agg_nested_dicts(): np.random.seed(1234) diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index f84a579247630..e551aceab762b 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -127,6 +127,17 @@ def test_agg(): tm.assert_frame_equal(result, expected, check_like=True) +@pytest.mark.parametrize( + "func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}] +) +def test_multi_axis_1_raises(func): + # GH#46904 + df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]}) + r = df.rolling(window=3, axis=1) + with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"): + r.agg(func) + + def test_agg_apply(raw): # passed lambda