Skip to content

Commit

Permalink
BUG: addtl fix for compat summary of groupby/resample with dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
jreback committed Feb 15, 2016
1 parent 8f1a318 commit 66c23aa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
23 changes: 14 additions & 9 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2526,7 +2526,8 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
return getattr(self, func_or_funcs)(*args, **kwargs)

if hasattr(func_or_funcs, '__iter__'):
ret = self._aggregate_multiple_funcs(func_or_funcs, _level)
ret = self._aggregate_multiple_funcs(func_or_funcs,
(_level or 0) + 1)
else:
cyfunc = self._is_cython_func(func_or_funcs)
if cyfunc and not args and not kwargs:
Expand All @@ -2546,6 +2547,18 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
if not self.as_index: # pragma: no cover
print('Warning, ignoring as_index=True')

# _level handled at higher
if not _level and isinstance(ret, dict):
from pandas import concat

# our result is a Series-like
if len(ret) == 1:
ret = concat([r for r in ret.values()],
axis=1)

# our result is a DataFrame like
else:
ret = concat(ret, axis=1)
return ret

agg = aggregate
Expand All @@ -2571,14 +2584,6 @@ def _aggregate_multiple_funcs(self, arg, _level):
columns.append(com._get_callable_name(f))
arg = lzip(columns, arg)

# for a ndim=1, disallow a nested dict for an aggregator as
# this is a mis-specification of the aggregations, via a
# specificiation error
# e.g. g['A'].agg({'A': ..., 'B': ...})
if self.name in columns and len(columns) > 1:
raise SpecificationError('invalid aggregation names specified '
'for selected objects')

results = {}
for name, func in arg:
obj = self
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,13 @@ def f():
'ra', 'std'), ('rb', 'mean'), ('rb', 'std')])
assert_frame_equal(result, expected, check_like=True)

# same name as the original column
# GH9052
expected = g['D'].agg({'result1': np.sum, 'result2': np.mean})
expected = expected.rename(columns={'result1': 'D'})
result = g['D'].agg({'D': np.sum, 'result2': np.mean})
assert_frame_equal(result, expected, check_like=True)

def test_multi_iter(self):
s = Series(np.arange(6))
k1 = np.array(['a', 'a', 'a', 'b', 'b', 'b'])
Expand Down
31 changes: 19 additions & 12 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,25 +419,32 @@ def test_agg_misc(self):
assert_frame_equal(result, expected, check_like=True)

# series like aggs
expected = pd.concat([t['A'].sum(),
t['A'].std()],
axis=1)
expected.columns = ['sum', 'std']

for t in [r, g]:
result = r['A'].agg({'A': ['sum', 'std']})
result = t['A'].agg({'A': ['sum', 'std']})
expected = pd.concat([t['A'].sum(),
t['A'].std()],
axis=1)
expected.columns = ['sum', 'std']

assert_frame_equal(result, expected, check_like=True)

expected = pd.concat([t['A'].agg(['sum', 'std']),
t['A'].agg(['mean', 'std'])],
axis=1)
expected.columns = pd.MultiIndex.from_tuples([('A', 'sum'),
('A', 'std'),
('B', 'mean'),
('B', 'std')])
result = t['A'].agg({'A': ['sum', 'std'], 'B': ['mean', 'std']})
assert_frame_equal(result, expected, check_like=True)

# errors
# invalid names in the agg specification
for t in [r, g]:

# invalid names in the agg specification
def f():
r['A'].agg({'A': ['sum', 'std'], 'B': ['mean', 'std']})
self.assertRaises(SpecificationError, f)

def f():
r[['A']].agg({'A': ['sum', 'std'], 'B': ['mean', 'std']})
r[['A']].agg({'A': ['sum', 'std'],
'B': ['mean', 'std']})
self.assertRaises(SpecificationError, f)

def test_agg_nested_dicts(self):
Expand Down

0 comments on commit 66c23aa

Please sign in to comment.