Skip to content

Commit 52a9bed

Browse files
Backport PR #47078 on branch 1.4.x (REGR: Raise NotImplementedError for agg with axis=1 and multiple funcs) (#47086)
Backport PR #47078: REGR: Raise NotImplementedError for agg with axis=1 and multiple funcs Co-authored-by: Richard Shadrach <[email protected]>
1 parent 59de03a commit 52a9bed

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
@@ -1393,3 +1393,14 @@ def test_groupby_complex_raises(func):
13931393
msg = "No matching signature found"
13941394
with pytest.raises(TypeError, match=msg):
13951395
data.groupby(data.index % 2).agg(func)
1396+
1397+
1398+
@pytest.mark.parametrize(
1399+
"func", [["min"], ["mean", "max"], {"b": "sum"}, {"b": "prod", "c": "median"}]
1400+
)
1401+
def test_multi_axis_1_raises(func):
1402+
# GH#46995
1403+
df = DataFrame({"a": [1, 1, 2], "b": [3, 4, 5], "c": [6, 7, 8]})
1404+
gb = df.groupby("a", axis=1)
1405+
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
1406+
gb.agg(func)

pandas/tests/resample/test_resample_api.py

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

515515

516+
@pytest.mark.parametrize(
517+
"func", [["min"], ["mean", "max"], {"A": "sum"}, {"A": "prod", "B": "median"}]
518+
)
519+
def test_multi_agg_axis_1_raises(func):
520+
# GH#46904
521+
np.random.seed(1234)
522+
index = date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D")
523+
index.name = "date"
524+
df = DataFrame(np.random.rand(10, 2), columns=list("AB"), index=index).T
525+
res = df.resample("M", axis=1)
526+
with pytest.raises(NotImplementedError, match="axis other than 0 is not supported"):
527+
res.agg(func)
528+
529+
516530
def test_agg_nested_dicts():
517531

518532
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():
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)