From 1a64912d7de67553d36171372e3b383839ad91a2 Mon Sep 17 00:00:00 2001 From: richard Date: Wed, 18 May 2022 23:48:03 -0400 Subject: [PATCH 1/2] 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 | 9 +++++++++ pandas/tests/resample/test_resample_api.py | 12 ++++++++++++ pandas/tests/window/test_api.py | 9 +++++++++ 5 files changed, 38 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 c04d0821fffdc..2b1288511035f 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 37b02571158b9..22db65fe3edd5 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1401,3 +1401,12 @@ 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 21ef078bcf418..878926b154173 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -553,6 +553,18 @@ 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 6dbcc8dfd00c0..52637fbf57788 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -127,6 +127,15 @@ def test_agg(step): 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 From 7dee2367f75d40aeacf587419554fda48d1553e7 Mon Sep 17 00:00:00 2001 From: richard Date: Fri, 20 May 2022 22:32:29 -0400 Subject: [PATCH 2/2] fix my precommit --- pandas/tests/groupby/aggregate/test_aggregate.py | 4 +++- pandas/tests/resample/test_resample_api.py | 4 +++- pandas/tests/window/test_api.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/aggregate/test_aggregate.py b/pandas/tests/groupby/aggregate/test_aggregate.py index 22db65fe3edd5..55aad403f1492 100644 --- a/pandas/tests/groupby/aggregate/test_aggregate.py +++ b/pandas/tests/groupby/aggregate/test_aggregate.py @@ -1403,7 +1403,9 @@ def test_groupby_complex_raises(func): data.groupby(data.index % 2).agg(func) -@pytest.mark.parametrize("func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]) +@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]}) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 878926b154173..04b629d089925 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -553,7 +553,9 @@ 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"}]) +@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) diff --git a/pandas/tests/window/test_api.py b/pandas/tests/window/test_api.py index 52637fbf57788..931e52e4efd18 100644 --- a/pandas/tests/window/test_api.py +++ b/pandas/tests/window/test_api.py @@ -127,7 +127,9 @@ def test_agg(step): tm.assert_frame_equal(result, expected, check_like=True) -@pytest.mark.parametrize("func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]) +@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]})