Skip to content

Commit 61efac7

Browse files
authored
REGR: Raise NotImplementedError for agg with axis=1 and multiple funcs (#47078)
1 parent f852589 commit 61efac7

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

doc/source/whatsnew/v1.4.3.rst

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Fixed regressions
1818
- Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`)
1919
- Fixed regression in :meth:`.Groupby.transform` and :meth:`.Groupby.agg` failing with ``engine="numba"`` when the index was a :class:`MultiIndex` (:issue:`46867`)
2020
- Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`)
21+
- 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`)
22+
- 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`)
2123

2224
.. ---------------------------------------------------------------------------
2325

pandas/core/apply.py

+6
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ def agg_list_like(self) -> DataFrame | Series:
325325
obj = self.obj
326326
arg = cast(List[AggFuncTypeBase], self.f)
327327

328+
if getattr(obj, "axis", 0) == 1:
329+
raise NotImplementedError("axis other than 0 is not supported")
330+
328331
if not isinstance(obj, SelectionMixin):
329332
# i.e. obj is Series or DataFrame
330333
selected_obj = obj
@@ -456,6 +459,9 @@ def agg_dict_like(self) -> DataFrame | Series:
456459
obj = self.obj
457460
arg = cast(AggFuncTypeDict, self.f)
458461

462+
if getattr(obj, "axis", 0) == 1:
463+
raise NotImplementedError("axis other than 0 is not supported")
464+
459465
if not isinstance(obj, SelectionMixin):
460466
# i.e. obj is Series or DataFrame
461467
selected_obj = obj

pandas/tests/groupby/aggregate/test_aggregate.py

+11
Original file line numberDiff line numberDiff line change
@@ -1401,3 +1401,14 @@ def test_groupby_complex_raises(func):
14011401
msg = "No matching signature found"
14021402
with pytest.raises(TypeError, match=msg):
14031403
data.groupby(data.index % 2).agg(func)
1404+
1405+
1406+
@pytest.mark.parametrize(
1407+
"func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]
1408+
)
1409+
def test_multi_axis_1_raises(func):
1410+
# GH#46995
1411+
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
1412+
gb = df.groupby("a", axis=1)
1413+
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
1414+
gb.agg(func)

pandas/tests/resample/test_resample_api.py

+14
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,20 @@ def test_agg_misc():
553553
t[["A"]].agg({"A": ["sum", "std"], "B": ["mean", "std"]})
554554

555555

556+
@pytest.mark.parametrize(
557+
"func", [["min"], ["mean", "max"], {"A": "sum"}, {"A": "prod", "B": "median"}]
558+
)
559+
def test_multi_agg_axis_1_raises(func):
560+
# GH#46904
561+
np.random.seed(1234)
562+
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
563+
index.name = "date"
564+
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T
565+
res = df.resample("M", axis=1)
566+
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
567+
res.agg(func)
568+
569+
556570
def test_agg_nested_dicts():
557571

558572
np.random.seed(1234)

pandas/tests/window/test_api.py

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ def test_agg(step):
127127
tm.assert_frame_equal(result, expected, check_like=True)
128128

129129

130+
@pytest.mark.parametrize(
131+
"func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]
132+
)
133+
def test_multi_axis_1_raises(func):
134+
# GH#46904
135+
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
136+
r = df.rolling(window=3, axis=1)
137+
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
138+
r.agg(func)
139+
140+
130141
def test_agg_apply(raw):
131142

132143
# passed lambda

0 commit comments

Comments
 (0)