Skip to content

BUG: SeriesGroupBy apply wrong name #46623

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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue comment is not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return result
else:
# GH #6265 #24880
result = self.obj._constructor(
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 1 addition & 2 deletions pandas/tests/groupby/test_value_counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 2 additions & 7 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)