Skip to content

Commit 2f7fdd0

Browse files
BUG: groupby apply on selected columns yielding scalar (GH13568) (pandas-dev#13585)
closes pandas-dev#13568
1 parent 5605f99 commit 2f7fdd0

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ Bug Fixes
491491
- Bug in ``PeriodIndex`` construction returning a ``float64`` index in some circumstances (:issue:`13067`)
492492
- Bug in ``.resample(..)`` with a ``PeriodIndex`` not changing its ``freq`` appropriately when empty (:issue:`13067`)
493493
- Bug in ``.resample(..)`` with a ``PeriodIndex`` not retaining its type or name with an empty ``DataFrame`` appropriately when empty (:issue:`13212`)
494+
- Bug in ``groupby(..).apply(..)`` when the passed function returns scalar values per group (:issue:`13468`).
494495
- Bug in ``groupby(..).resample(..)`` where passing some keywords would raise an exception (:issue:`13235`)
495496
- Bug in ``.tz_convert`` on a tz-aware ``DateTimeIndex`` that relied on index being sorted for correct results (:issue:`13306`)
496497
- Bug in ``.tz_localize`` with ``dateutil.tz.tzlocal`` may return incorrect result (:issue:`13583`)

pandas/core/groupby.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -3403,11 +3403,14 @@ def first_non_None_value(values):
34033403

34043404
return self._reindex_output(result)
34053405

3406+
# values are not series or array-like but scalars
34063407
else:
34073408
# only coerce dates if we find at least 1 datetime
34083409
coerce = True if any([isinstance(x, Timestamp)
34093410
for x in values]) else False
3410-
return (Series(values, index=key_index, name=self.name)
3411+
# self.name not passed through to Series as the result
3412+
# should not take the name of original selection of columns
3413+
return (Series(values, index=key_index)
34113414
._convert(datetime=True,
34123415
coerce=coerce))
34133416

pandas/tests/test_groupby.py

+10
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,16 @@ def test_apply_series_yield_constant(self):
25842584
result = self.df.groupby(['A', 'B'])['C'].apply(len)
25852585
self.assertEqual(result.index.names[:2], ('A', 'B'))
25862586

2587+
def test_apply_frame_yield_constant(self):
2588+
# GH13568
2589+
result = self.df.groupby(['A', 'B']).apply(len)
2590+
self.assertTrue(isinstance(result, Series))
2591+
self.assertIsNone(result.name)
2592+
2593+
result = self.df.groupby(['A', 'B'])[['C', 'D']].apply(len)
2594+
self.assertTrue(isinstance(result, Series))
2595+
self.assertIsNone(result.name)
2596+
25872597
def test_apply_frame_to_series(self):
25882598
grouped = self.df.groupby(['A', 'B'])
25892599
result = grouped.apply(len)

0 commit comments

Comments
 (0)