Skip to content

Commit

Permalink
FIX: Allow aggregate to return dictionaries again #16741 (#16752)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianWilhelm authored and jreback committed Jun 30, 2017
1 parent 6ae92a8 commit 06fc667
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Performance Improvements
Bug Fixes
~~~~~~~~~

- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)

Conversion
^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/src/reduce.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ cdef class SeriesGrouper:
cdef inline _extract_result(object res):
""" extract the result object, it might be a 0-dim ndarray
or a len-1 0-dim, or a scalar """
if hasattr(res, 'values'):
if hasattr(res, 'values') and isinstance(res.values, np.ndarray):
res = res.values
if not np.isscalar(res):
if isinstance(res, np.ndarray):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,16 @@ def test_cython_agg_frame_columns(self):
df.groupby(level=0, axis='columns').mean()
df.groupby(level=0, axis='columns').mean()

def test_cython_agg_return_dict(self):
# GH 16741
ts = self.df.groupby('A')['B'].agg(
lambda x: x.value_counts().to_dict())
expected = Series([{'two': 1, 'one': 1, 'three': 1},
{'two': 2, 'one': 2, 'three': 1}],
index=Index(['bar', 'foo'], name='A'),
name='B')
assert_series_equal(ts, expected)

def test_cython_fail_agg(self):
dr = bdate_range('1/1/2000', periods=50)
ts = Series(['A', 'B', 'C', 'D', 'E'] * 10, index=dr)
Expand Down

0 comments on commit 06fc667

Please sign in to comment.