From c863f89a7ae7fa8a80fc321b0c6e35ad23fc13fb Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Fri, 25 Nov 2022 12:35:12 +0000 Subject: [PATCH 1/3] dont set name if dataframe --- doc/source/whatsnew/v1.5.3.rst | 1 + pandas/core/groupby/generic.py | 3 ++- pandas/tests/groupby/test_apply.py | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.3.rst b/doc/source/whatsnew/v1.5.3.rst index e7428956c50b5..e0bcd81805cc1 100644 --- a/doc/source/whatsnew/v1.5.3.rst +++ b/doc/source/whatsnew/v1.5.3.rst @@ -16,6 +16,7 @@ Fixed regressions - Fixed performance regression in :meth:`Series.isin` when ``values`` is empty (:issue:`49839`) - Fixed regression in :meth:`DataFrameGroupBy.transform` when used with ``as_index=False`` (:issue:`49834`) - Enforced reversion of ``color`` as an alias for ``c`` and ``size`` as an alias for ``s`` in function :meth:`DataFrame.plot.scatter` (:issue:`49732`) +- Fixed regression in :meth:`SeriesGroupBy.apply` setting a ``name`` attribute on the result if the result was a :class:`DataFrame` (:issue:`49907`) - .. --------------------------------------------------------------------------- diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index a9ef1fca7e8e7..7dce4755e861e 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -401,7 +401,8 @@ def _wrap_applied_output( not_indexed_same=not_indexed_same, is_transform=is_transform, ) - result.name = self.obj.name + if isinstance(result, Series): + result.name = self.obj.name return result else: # GH #6265 #24880 diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index af523928e0bc8..a130865bb5534 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -334,6 +334,7 @@ def f(piece): result = grouped.apply(f) assert isinstance(result, DataFrame) + assert not hasattr(result, "name") tm.assert_index_equal(result.index, ts.index) From 191b4cb5179639b5b612632cabe1202293597b33 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Fri, 25 Nov 2022 16:16:57 +0000 Subject: [PATCH 2/3] use ndim in check, add gh reference to test --- pandas/core/groupby/generic.py | 2 +- pandas/tests/groupby/test_apply.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 7dce4755e861e..820155eadd1ac 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -401,7 +401,7 @@ def _wrap_applied_output( not_indexed_same=not_indexed_same, is_transform=is_transform, ) - if isinstance(result, Series): + if result.ndim == 1: result.name = self.obj.name return result else: diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index a130865bb5534..0d5ab9a4f1acc 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -334,7 +334,7 @@ def f(piece): result = grouped.apply(f) assert isinstance(result, DataFrame) - assert not hasattr(result, "name") + assert not hasattr(result, "name") # GH49907 tm.assert_index_equal(result.index, ts.index) From 962d02abcb1716b4621681bcee4090e48c54a5a0 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Fri, 25 Nov 2022 16:45:51 +0000 Subject: [PATCH 3/3] back to isinstance --- 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 820155eadd1ac..7dce4755e861e 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -401,7 +401,7 @@ def _wrap_applied_output( not_indexed_same=not_indexed_same, is_transform=is_transform, ) - if result.ndim == 1: + if isinstance(result, Series): result.name = self.obj.name return result else: