Skip to content

Commit 06fc667

Browse files
FlorianWilhelmjreback
authored andcommitted
FIX: Allow aggregate to return dictionaries again #16741 (#16752)
1 parent 6ae92a8 commit 06fc667

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

Diff for: doc/source/whatsnew/v0.21.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ Performance Improvements
9292
Bug Fixes
9393
~~~~~~~~~
9494

95+
- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
96+
9597
Conversion
9698
^^^^^^^^^^
9799

Diff for: pandas/_libs/src/reduce.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ cdef class SeriesGrouper:
419419
cdef inline _extract_result(object res):
420420
""" extract the result object, it might be a 0-dim ndarray
421421
or a len-1 0-dim, or a scalar """
422-
if hasattr(res, 'values'):
422+
if hasattr(res, 'values') and isinstance(res.values, np.ndarray):
423423
res = res.values
424424
if not np.isscalar(res):
425425
if isinstance(res, np.ndarray):

Diff for: pandas/tests/groupby/test_aggregate.py

+10
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,16 @@ def test_cython_agg_frame_columns(self):
611611
df.groupby(level=0, axis='columns').mean()
612612
df.groupby(level=0, axis='columns').mean()
613613

614+
def test_cython_agg_return_dict(self):
615+
# GH 16741
616+
ts = self.df.groupby('A')['B'].agg(
617+
lambda x: x.value_counts().to_dict())
618+
expected = Series([{'two': 1, 'one': 1, 'three': 1},
619+
{'two': 2, 'one': 2, 'three': 1}],
620+
index=Index(['bar', 'foo'], name='A'),
621+
name='B')
622+
assert_series_equal(ts, expected)
623+
614624
def test_cython_fail_agg(self):
615625
dr = bdate_range('1/1/2000', periods=50)
616626
ts = Series(['A', 'B', 'C', 'D', 'E'] * 10, index=dr)

0 commit comments

Comments
 (0)