Skip to content

BUG: Fix unintentionally added suffix in DataFrame.apply/agg and Series.apply/agg #36231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
- 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`)
- Fixed regression in :class:`IntegerArray` unary plus and minus operations raising a ``TypeError`` (:issue:`36063`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a tuple (:issue:`35534`)
- Fixed regression in :meth:`Series.__getitem__` incorrectly raising when the input was a frozenset (:issue:`35747`)
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def reconstruct_func(
Examples
--------
>>> reconstruct_func(None, **{"foo": ("col", "min")})
(True, defaultdict(None, {'col': ['min']}), ('foo',), array([0]))
(True, defaultdict(<class 'list'>, {'col': ['min']}), ('foo',), array([0]))

>>> reconstruct_func("min")
(False, 'min', None, None)
Expand All @@ -87,7 +87,6 @@ def reconstruct_func(

if relabeling:
func, columns, order = normalize_keyword_aggregation(kwargs)
func = maybe_mangle_lambdas(func)

return relabeling, func, columns, order

Expand Down
1 change: 1 addition & 0 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
return self.obj._constructor(result, index=index, columns=data.columns)

relabeling, func, columns, order = reconstruct_func(func, **kwargs)
func = maybe_mangle_lambdas(func)

result, how = self._aggregate(func, *args, **kwargs)
if how is None:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,14 @@ def test_apply_empty_list_reduce():
result = df.apply(lambda x: [], result_type="reduce")
expected = pd.Series({"a": [], "b": []}, dtype=object)
tm.assert_series_equal(result, expected)


def test_apply_no_suffix_index():
# GH36189
pdf = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
result = pdf.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
expected = pd.DataFrame(
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
)

tm.assert_frame_equal(result, expected)
15 changes: 15 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,3 +1153,18 @@ def test_nonagg_agg():
expected = g.agg("cumsum")

tm.assert_frame_equal(result, expected)


def test_agg_no_suffix_index():
# GH36189
df = pd.DataFrame([[4, 9]] * 3, columns=["A", "B"])
result = df.agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
expected = pd.DataFrame(
{"A": [12, 12, 12], "B": [27, 27, 27]}, index=["sum", "<lambda>", "<lambda>"]
)
tm.assert_frame_equal(result, expected)

# test Series case
result = df["A"].agg(["sum", lambda x: x.sum(), lambda x: x.sum()])
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"], name="A")
tm.assert_series_equal(result, expected)
8 changes: 8 additions & 0 deletions pandas/tests/series/apply/test_series_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,14 @@ def test_agg_cython_table_raises(self, series, func, expected):
# e.g. Series('a b'.split()).cumprod() will raise
series.agg(func)

def test_series_apply_no_suffix_index(self):
# GH36189
s = pd.Series([4] * 3)
result = s.apply(["sum", lambda x: x.sum(), lambda x: x.sum()])
expected = pd.Series([12, 12, 12], index=["sum", "<lambda>", "<lambda>"])

tm.assert_series_equal(result, expected)


class TestSeriesMap:
def test_map(self, datetime_series):
Expand Down