Skip to content

Commit 72a0f8a

Browse files
ma3dayehoshuadimarsky
authored andcommitted
BUG: SeriesGroupBy apply wrong name (pandas-dev#46623)
1 parent 4a11161 commit 72a0f8a

File tree

6 files changed

+18
-11
lines changed

6 files changed

+18
-11
lines changed

doc/source/whatsnew/v1.5.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,7 @@ Groupby/resample/rolling
602602
- Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`)
603603
- Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`)
604604
- Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`)
605+
- Bug in :meth:`SeriesGroupBy.apply` would incorrectly name its result when there was a unique group (:issue:`46369`)
605606
- Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`)
606607
- Bug in :meth:`Grouper.__repr__` where ``dropna`` was not included. Now it is (:issue:`46754`)
607608

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,7 @@ def get_cython_func(arg: Callable) -> str | None:
612612

613613
def is_builtin_func(arg):
614614
"""
615-
if we define an builtin function for this argument, return it,
615+
if we define a builtin function for this argument, return it,
616616
otherwise return the arg
617617
"""
618618
return _builtin_table.get(arg, arg)

pandas/core/groupby/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -397,11 +397,13 @@ def _wrap_applied_output(
397397
res_ser.name = self.obj.name
398398
return res_ser
399399
elif isinstance(values[0], (Series, DataFrame)):
400-
return self._concat_objects(
400+
result = self._concat_objects(
401401
values,
402402
not_indexed_same=not_indexed_same,
403403
override_group_keys=override_group_keys,
404404
)
405+
result.name = self.obj.name
406+
return result
405407
else:
406408
# GH #6265 #24880
407409
result = self.obj._constructor(

pandas/tests/groupby/test_apply.py

+10
Original file line numberDiff line numberDiff line change
@@ -1320,3 +1320,13 @@ def test_apply_str_with_args(df, args, kwargs):
13201320
result = gb.apply("sum", *args, **kwargs)
13211321
expected = gb.sum(numeric_only=True)
13221322
tm.assert_frame_equal(result, expected)
1323+
1324+
1325+
@pytest.mark.parametrize("name", ["some_name", None])
1326+
def test_result_name_when_one_group(name):
1327+
# GH 46369
1328+
ser = Series([1, 2], name=name)
1329+
result = ser.groupby(["a", "a"], group_keys=False).apply(lambda x: x)
1330+
expected = Series([1, 2], name=name)
1331+
1332+
tm.assert_series_equal(result, expected)

pandas/tests/groupby/test_value_counts.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,11 @@ def test_series_groupby_value_counts_on_categorical():
183183
),
184184
]
185185
),
186-
name=0,
187186
)
188187

189188
# Expected:
190189
# 0 a 1
191190
# b 0
192-
# Name: 0, dtype: int64
191+
# dtype: int64
193192

194193
tm.assert_series_equal(result, expected)

pandas/tests/groupby/transform/test_transform.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1511,10 +1511,5 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func)
15111511
msg = f"{transformation_func} is deprecated"
15121512
with tm.assert_produces_warning(warn, match=msg):
15131513
result = gb.transform(transformation_func, *args)
1514-
if dropna and transformation_func == "fillna":
1515-
# GH#46369 - result name is the group; remove this block when fixed.
1516-
tm.assert_equal(result, expected, check_names=False)
1517-
# This should be None
1518-
assert result.name == 1.0
1519-
else:
1520-
tm.assert_equal(result, expected)
1514+
1515+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)