diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 98d56bac402ac..97ff3a9af6e26 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -602,6 +602,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`) - Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`) Reshaping 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) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 8d9b94f242e33..06a1aed8e3b09 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 + return result else: # GH #6265 #24880 result = self.obj._constructor( diff --git a/pandas/tests/groupby/test_apply.py b/pandas/tests/groupby/test_apply.py index 9cb64766a1079..4cfc3ea41543b 100644 --- a/pandas/tests/groupby/test_apply.py +++ b/pandas/tests/groupby/test_apply.py @@ -1320,3 +1320,13 @@ 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) + + +@pytest.mark.parametrize("name", ["some_name", None]) +def test_result_name_when_one_group(name): + # GH 46369 + ser = Series([1, 2], name=name) + result = ser.groupby(["a", "a"], group_keys=False).apply(lambda x: x) + expected = Series([1, 2], name=name) + + tm.assert_series_equal(result, expected) 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)