Skip to content

Commit ed09b58

Browse files
BUG: Fix 58807 (#59243)
* throw when frame apply is given invalid axis+func * test that frame_apply throws on invalid func+axis * add a note on unsupported func+axis combination for frame_apply * add bug fix to release notes * fix based on review comments * test also with named axis * Update doc/source/whatsnew/v3.0.0.rst Co-authored-by: Matthew Roeschke <[email protected]> * fix rst --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent d6724bc commit ed09b58

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Other enhancements
3232
- :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`)
3333
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
3434
- :func:`read_stata` now returns ``datetime64`` resolutions better matching those natively stored in the stata format (:issue:`55642`)
35+
- :meth:`DataFrame.agg` called with ``axis=1`` and a ``func`` which relabels the result index now raises a ``NotImplementedError`` (:issue:`58807`).
3536
- :meth:`Styler.set_tooltips` provides alternative method to storing tooltips by using title attribute of td elements. (:issue:`56981`)
3637
- Allow dictionaries to be passed to :meth:`pandas.Series.str.replace` via ``pat`` parameter (:issue:`51748`)
3738
- Support passing a :class:`Series` input to :func:`json_normalize` that retains the :class:`Series` :class:`Index` (:issue:`51452`)

pandas/core/apply.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,19 @@ def frame_apply(
9090
kwargs=None,
9191
) -> FrameApply:
9292
"""construct and return a row or column based frame apply object"""
93+
_, func, columns, _ = reconstruct_func(func, **kwargs)
94+
9395
axis = obj._get_axis_number(axis)
9496
klass: type[FrameApply]
9597
if axis == 0:
9698
klass = FrameRowApply
9799
elif axis == 1:
100+
if columns:
101+
raise NotImplementedError(
102+
f"Named aggregation is not supported when {axis=}."
103+
)
98104
klass = FrameColumnApply
99105

100-
_, func, _, _ = reconstruct_func(func, **kwargs)
101-
assert func is not None
102-
103106
return klass(
104107
obj,
105108
func,

pandas/core/shared_docs.py

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
for more details.
5050
5151
A passed user-defined-function will be passed a Series for evaluation.
52+
53+
If ``func`` defines an index relabeling, ``axis`` must be ``0`` or ``index``.
5254
{examples}"""
5355

5456
_shared_docs["compare"] = """

pandas/tests/apply/test_frame_apply.py

+8
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,14 @@ def test_agg_reduce(axis, float_frame):
13301330
tm.assert_frame_equal(result, expected)
13311331

13321332

1333+
def test_named_agg_reduce_axis1_raises(float_frame):
1334+
name1, name2 = float_frame.axes[0].unique()[:2].sort_values()
1335+
msg = "Named aggregation is not supported when axis=1."
1336+
for axis in [1, "columns"]:
1337+
with pytest.raises(NotImplementedError, match=msg):
1338+
float_frame.agg(row1=(name1, "sum"), row2=(name2, "max"), axis=axis)
1339+
1340+
13331341
def test_nuiscance_columns():
13341342
# GH 15015
13351343
df = DataFrame(

0 commit comments

Comments
 (0)