Skip to content

Commit 752cd42

Browse files
BUG: Fixe unintentionally added suffix in DataFrame.apply/agg and Series.apply/agg (#36231)
1 parent 068d1b5 commit 752cd42

File tree

6 files changed

+37
-2
lines changed

6 files changed

+37
-2
lines changed

doc/source/whatsnew/v1.1.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed regression in :meth:`DataFrame.agg`, :meth:`DataFrame.apply`, :meth:`Series.agg`, and :meth:`Series.apply` where internal suffix is exposed to the users when no relabelling is applied (:issue:`36189`)
1718
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)
1819
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
1920
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)

pandas/core/aggregation.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def reconstruct_func(
6363
Examples
6464
--------
6565
>>> reconstruct_func(None, **{"foo": ("col", "min")})
66-
(True, defaultdict(None, {'col': ['min']}), ('foo',), array([0]))
66+
(True, defaultdict(<class 'list'>, {'col': ['min']}), ('foo',), array([0]))
6767
6868
>>> reconstruct_func("min")
6969
(False, 'min', None, None)
@@ -87,7 +87,6 @@ def reconstruct_func(
8787

8888
if relabeling:
8989
func, columns, order = normalize_keyword_aggregation(kwargs)
90-
func = maybe_mangle_lambdas(func)
9190

9291
return relabeling, func, columns, order
9392

pandas/core/groupby/generic.py

+1
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
951951
return self.obj._constructor(result, index=index, columns=data.columns)
952952

953953
relabeling, func, columns, order = reconstruct_func(func, **kwargs)
954+
func = maybe_mangle_lambdas(func)
954955

955956
result, how = self._aggregate(func, *args, **kwargs)
956957
if how is None:

pandas/tests/frame/apply/test_frame_apply.py

+11
Original file line numberDiff line numberDiff line change
@@ -1534,3 +1534,14 @@ def test_apply_empty_list_reduce():
15341534
result = df.apply(lambda x: [], result_type="reduce")
15351535
expected = pd.Series({"a": [], "b": []}, dtype=object)
15361536
tm.assert_series_equal(result, expected)
1537+
1538+
1539+
def test_apply_no_suffix_index():
1540+
# GH36189
1541+
pdf = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
1542+
result = pdf.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
1543+
expected = pd.DataFrame(
1544+
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
1545+
)
1546+
1547+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/aggregate/test_aggregate.py

+15
Original file line numberDiff line numberDiff line change
@@ -1153,3 +1153,18 @@ def test_nonagg_agg():
11531153
expected = g.agg("cumsum")
11541154

11551155
tm.assert_frame_equal(result, expected)
1156+
1157+
1158+
def test_agg_no_suffix_index():
1159+
# GH36189
1160+
df = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
1161+
result = df.agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
1162+
expected = pd.DataFrame(
1163+
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
1164+
)
1165+
tm.assert_frame_equal(result, expected)
1166+
1167+
# test Series case
1168+
result = df["A"].agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
1169+
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"], name="A")
1170+
tm.assert_series_equal(result, expected)

pandas/tests/series/apply/test_series_apply.py

+8
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ def test_agg_cython_table_raises(self, series, func, expected):
445445
# e.g. Series('a b'.split()).cumprod() will raise
446446
series.agg(func)
447447

448+
def test_series_apply_no_suffix_index(self):
449+
# GH36189
450+
s = pd.Series([4] * 3)
451+
result = s.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
452+
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"])
453+
454+
tm.assert_series_equal(result, expected)
455+
448456

449457
class TestSeriesMap:
450458
def test_map(self, datetime_series):

0 commit comments

Comments
 (0)