Skip to content

Commit 26e0f9a

Browse files
fix based on review comments
1 parent bd1ce77 commit 26e0f9a

File tree

4 files changed

+11
-16
lines changed

4 files changed

+11
-16
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ enhancement2
2828

2929
Other enhancements
3030
^^^^^^^^^^^^^^^^^^
31+
- Fix confusing chain of ``KeyError`` exceptions (:issue:`58807`) when :meth:`DataFrame.agg` is called with ``axis=1`` and a ``func`` which relabels the result index.
3132
- :class:`pandas.api.typing.FrozenList` is available for typing the outputs of :attr:`MultiIndex.names`, :attr:`MultiIndex.codes` and :attr:`MultiIndex.levels` (:issue:`58237`)
3233
- :class:`pandas.api.typing.SASReader` is available for typing the output of :func:`read_sas` (:issue:`55689`)
3334
- :func:`DataFrame.to_excel` now raises an ``UserWarning`` when the character count in a cell exceeds Excel's limitation of 32767 characters (:issue:`56954`)
@@ -116,18 +117,9 @@ These improvements also fixed certain bugs in groupby:
116117
- :meth:`.DataFrameGroupBy.sum` would have incorrect values when there are multiple groupings, unobserved groups, and non-numeric data (:issue:`43891`)
117118
- :meth:`.DataFrameGroupBy.value_counts` would produce incorrect results when used with some categorical and some non-categorical groupings and ``observed=False`` (:issue:`56016`)
118119

119-
.. _whatsnew_300.notable_bug_fixes.improved_error_message_agg:
120+
.. _whatsnew_300.notable_bug_fixes.notable_bug_fix2:
120121

121-
Improved error message for :meth:`DataFrame.agg`
122-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123-
124-
When :meth:`DataFrame.agg` is called with ``axis=1`` and a ``func`` which relabels the result index, the :func:`relabel_result` function iterates over result columns rather than the result rows, causing a confusing chain of ``KeyError`` exceptions (:issue:`58807`).
125-
126-
This error is now handled in :func:`frame_apply` by throwing a ``NotImplementedError`` with a more explicit error message.
127-
128-
.. _whatsnew_300.notable_bug_fixes.notable_bug_fix3:
129-
130-
notable_bug_fix3
122+
notable_bug_fix2
131123
^^^^^^^^^^^^^^^^
132124

133125
.. ---------------------------------------------------------------------------

pandas/core/apply.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ def frame_apply(
9999
elif axis == 1:
100100
if columns:
101101
raise NotImplementedError(
102-
"func given to frame_apply cannot contain "
103-
"an index relabeling when axis is 1"
102+
f"Named aggregation is not supported when {axis=}."
104103
)
105104
klass = FrameColumnApply
106105

pandas/core/shared_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
5151
A passed user-defined-function will be passed a Series for evaluation.
5252
53-
If `func` defines an index relabeling, `axis` must be `0` or `index`.
53+
If ``func`` defines an index relabeling, ``axis`` must be ``0`` or ``index``.
5454
{examples}"""
5555

5656
_shared_docs["compare"] = """

pandas/tests/apply/test_frame_apply.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,9 +1329,13 @@ def test_agg_reduce(axis, float_frame):
13291329
expected = expected.T if axis in {1, "columns"} else expected
13301330
tm.assert_frame_equal(result, expected)
13311331

1332-
msg = "func given to frame_apply cannot contain an index relabeling when axis is 1"
1332+
1333+
def test_named_agg_reduce_axis1_raises(float_frame):
1334+
axis = 1
1335+
name1, name2 = float_frame.axes[0].unique()[:2].sort_values()
1336+
msg = f"Named aggregation is not supported when {axis=}."
13331337
with pytest.raises(NotImplementedError, match=msg):
1334-
float_frame.agg(row1=(name1, "sum"), row2=(name2, "max"), axis=1)
1338+
float_frame.agg(row1=(name1, "sum"), row2=(name2, "max"), axis=axis)
13351339

13361340

13371341
def test_nuiscance_columns():

0 commit comments

Comments
 (0)