From 0f109d7db034c61e43d1aecb641578049a077d99 Mon Sep 17 00:00:00 2001 From: Charles Blackmon-Luca <20627856+charlesbluca@users.noreply.github.com> Date: Mon, 9 Aug 2021 16:35:41 -0400 Subject: [PATCH] Apply series name to result of `SeriesGroupby.apply()` (#8939) Closes #8899 Applies the name of `SeriesGroupby.obj` to the results of a `SeriesGroupby.apply()` operation; originally, this would be left as `None`. Authors: - Charles Blackmon-Luca (https://github.com/charlesbluca) Approvers: - Sheilah Kirui (https://github.com/skirui-source) - Michael Wang (https://github.com/isVoid) URL: https://github.com/rapidsai/cudf/pull/8939 --- python/cudf/cudf/core/groupby/groupby.py | 8 ++++++++ python/cudf/cudf/tests/test_groupby.py | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/python/cudf/cudf/core/groupby/groupby.py b/python/cudf/cudf/core/groupby/groupby.py index d51ae78e853..540fe665e33 100644 --- a/python/cudf/cudf/core/groupby/groupby.py +++ b/python/cudf/cudf/core/groupby/groupby.py @@ -1222,6 +1222,14 @@ def agg(self, func): return result + def apply(self, func): + result = super().apply(func) + + # apply Series name to result + result.name = self.obj.name + + return result + class Grouper(object): def __init__(self, key=None, level=None): diff --git a/python/cudf/cudf/tests/test_groupby.py b/python/cudf/cudf/tests/test_groupby.py index fb8379315a9..e45963c1321 100644 --- a/python/cudf/cudf/tests/test_groupby.py +++ b/python/cudf/cudf/tests/test_groupby.py @@ -2104,3 +2104,13 @@ def test_groupby_first(data, agg): expect = pdf.groupby("a").agg(agg) got = gdf.groupby("a").agg(agg) assert_groupby_results_equal(expect, got, check_dtype=False) + + +def test_groupby_apply_series_name(): + def foo(x): + return x.sum() + + got = make_frame(DataFrame, 3).groupby("x").y.apply(foo) + expect = make_frame(pd.DataFrame, 3).groupby("x").y.apply(foo) + + assert expect.name == got.name