From da22095c53ee39fc4c3f76eaaeeb207489944e2a Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 3 Apr 2022 09:54:22 +0200 Subject: [PATCH 1/7] BUG-46369: add test --- pandas/tests/groupby/test_apply.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 9cb64766a1079..1cb91d58c34e6 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1320,3 +1320,15 @@ def test_apply_str_with_args(df, args, kwargs): result = gb.apply("sum", *args, **kwargs) expected = gb.sum(numeric_only=True) tm.assert_frame_equal(result, expected) + + +def test_result_name_when_one_group(): + # GH 46369 + names = ("some_name", None) + + for name in names: + ser = Series([0, 1], name=name) + result = ser.groupby(["a", "a"]).apply(lambda x: x).name + expected = name + + assert result == expected From 0c79ef852e87fae7e6823665612f79a63fc434fc Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 3 Apr 2022 12:30:55 +0200 Subject: [PATCH 2/7] BUG-46369: Set name in SeriesGroupBy._wrap_applied_output --- pandas/core/groupby/generic.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8d9b94f242e33..f0f00be6a85a5 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -397,11 +397,13 @@ def _wrap_applied_output( res_ser.name = self.obj.name return res_ser elif isinstance(values[0], (Series, DataFrame)): - return self._concat_objects( + result = self._concat_objects( values, not_indexed_same=not_indexed_same, override_group_keys=override_group_keys, ) + result.name = self.obj.name # GH #46369 + return result else: # GH #6265 #24880 result = self.obj._constructor( From 9caa7a94a7495457ee2cd2ce3ea4cf6f5e684cc1 Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 3 Apr 2022 12:29:53 +0200 Subject: [PATCH 3/7] BUG-46369: Update other impacted tests --- pandas/tests/groupby/test_value_counts.py | 3 +-- pandas/tests/groupby/transform/test_transform.py | 9 ++------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/pandas/tests/groupby/test_value_counts.py b/pandas/tests/groupby/test_value_counts.py index d38ba84cd1397..577a72d3f5090 100644 --- a/pandas/tests/groupby/test_value_counts.py +++ b/pandas/tests/groupby/test_value_counts.py @@ -183,12 +183,11 @@ def test_series_groupby_value_counts_on_categorical(): ), ] ), - name=0, ) # Expected: # 0 a 1 # b 0 - # Name: 0, dtype: int64 + # dtype: int64 tm.assert_series_equal(result, expected) diff --git a/pandas/tests/groupby/transform/test_transform.py b/pandas/tests/groupby/transform/test_transform.py index a77c95e30ab43..237d6a96f39ec 100644 --- a/pandas/tests/groupby/transform/test_transform.py +++ b/pandas/tests/groupby/transform/test_transform.py @@ -1511,10 +1511,5 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func) msg = f"{transformation_func} is deprecated" with tm.assert_produces_warning(warn, match=msg): result = gb.transform(transformation_func, *args) - if dropna and transformation_func == "fillna": - # GH#46369 - result name is the group; remove this block when fixed. - tm.assert_equal(result, expected, check_names=False) - # This should be None - assert result.name == 1.0 - else: - tm.assert_equal(result, expected) + + tm.assert_equal(result, expected) From b484a57e32152d920abfa4e671172b48302be372 Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 3 Apr 2022 12:30:29 +0200 Subject: [PATCH 4/7] BUG-46369: typo --- pandas/core/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/common.py b/pandas/core/common.py index 6c3d2c91ab012..90f665362ef56 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -612,7 +612,7 @@ def get_cython_func(arg: Callable) -> str | None: def is_builtin_func(arg): """ - if we define an builtin function for this argument, return it, + if we define a builtin function for this argument, return it, otherwise return the arg """ return _builtin_table.get(arg, arg) From bae79e4b57e3a715926353e3a9935b2df56c6715 Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 3 Apr 2022 13:39:25 +0200 Subject: [PATCH 5/7] BUG-46369: update whatsnew --- doc/source/whatsnew/v1.5.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 358d9447b131d..ccf1dcc5705dd 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -599,6 +599,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`) - Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`) - Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`) +- Bug in :meth:`SeriesGroupBy.apply` would incorrectly name its result when there was a unique group (:issue:`46369`) - Reshaping From 7d2198d6e1a5aae73dbda011fa0a36fad7756dd5 Mon Sep 17 00:00:00 2001 From: Luis Pinto Date: Wed, 6 Apr 2022 21:16:46 +0200 Subject: [PATCH 6/7] BUG-46369: CR on test --- pandas/tests/groupby/test_apply.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 1cb91d58c34e6..4cfc3ea41543b 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1322,13 +1322,11 @@ def test_apply_str_with_args(df, args, kwargs): tm.assert_frame_equal(result, expected) -def test_result_name_when_one_group(): +@pytest.mark.parametrize("name", ["some_name", None]) +def test_result_name_when_one_group(name): # GH 46369 - names = ("some_name", None) + ser = Series([1, 2], name=name) + result = ser.groupby(["a", "a"], group_keys=False).apply(lambda x: x) + expected = Series([1, 2], name=name) - for name in names: - ser = Series([0, 1], name=name) - result = ser.groupby(["a", "a"]).apply(lambda x: x).name - expected = name - - assert result == expected + tm.assert_series_equal(result, expected) From f5b5b813a547ff52b4921d344c2a5d3ce7b88aa5 Mon Sep 17 00:00:00 2001 From: lpc Date: Sun, 17 Apr 2022 19:50:21 +0200 Subject: [PATCH 7/7] BUG-46369: CR rm comment --- pandas/core/groupby/generic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index f0f00be6a85a5..06a1aed8e3b09 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -402,7 +402,7 @@ def _wrap_applied_output( not_indexed_same=not_indexed_same, override_group_keys=override_group_keys, ) - result.name = self.obj.name # GH #46369 + result.name = self.obj.name return result else: # GH #6265 #24880