Skip to content

Commit 6f420a0

Browse files
charlesdong1991simonjayhawkins
authored andcommitted
Backport PR pandas-dev#36231: BUG: Fixe unintentionally added suffix in DataFrame.apply/agg and Series.apply/agg
1 parent ecb5e40 commit 6f420a0

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-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

+2
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,8 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
946946
func, *args, engine=engine, engine_kwargs=engine_kwargs, **kwargs
947947
)
948948

949+
func = maybe_mangle_lambdas(func)
950+
949951
result, how = self._aggregate(func, *args, **kwargs)
950952
if how is None:
951953
return result

pandas/tests/frame/apply/test_frame_apply.py

+11
Original file line numberDiff line numberDiff line change
@@ -1550,3 +1550,14 @@ def test_apply_empty_list_reduce():
15501550
result = df.apply(lambda x: [], result_type="reduce")
15511551
expected = pd.Series({"a": [], "b": []}, dtype=object)
15521552
tm.assert_series_equal(result, expected)
1553+
1554+
1555+
def test_apply_no_suffix_index():
1556+
# GH36189
1557+
pdf = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
1558+
result = pdf.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
1559+
expected = pd.DataFrame(
1560+
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
1561+
)
1562+
1563+
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/aggregate/test_aggregate.py

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

10761076
tm.assert_frame_equal(result, expected)
1077+
1078+
1079+
def test_agg_no_suffix_index():
1080+
# GH36189
1081+
df = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
1082+
result = df.agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
1083+
expected = pd.DataFrame(
1084+
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
1085+
)
1086+
tm.assert_frame_equal(result, expected)
1087+
1088+
# test Series case
1089+
result = df["A"].agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
1090+
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"], name="A")
1091+
tm.assert_series_equal(result, expected)

pandas/tests/series/apply/test_series_apply.py

+8
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,14 @@ def test_transform_none_to_type(self):
471471
with pytest.raises(TypeError, match=msg):
472472
df.transform({"a": int})
473473

474+
def test_series_apply_no_suffix_index(self):
475+
# GH36189
476+
s = pd.Series([4] * 3)
477+
result = s.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
478+
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"])
479+
480+
tm.assert_series_equal(result, expected)
481+
474482

475483
class TestSeriesMap:
476484
def test_map(self, datetime_series):

0 commit comments

Comments
 (0)