From 522dd79cd75328ad3a51c3ee8b3ef0e5640ed359 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Mon, 15 Feb 2016 17:11:34 -0500 Subject: [PATCH] API: consistency in aggregate groupby results closes #12334 --- doc/source/whatsnew/v0.18.0.txt | 2 +- pandas/core/groupby.py | 10 +--------- pandas/tests/test_groupby.py | 28 +++++++++++++++++++++++++++ pandas/tseries/tests/test_resample.py | 4 ++-- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/doc/source/whatsnew/v0.18.0.txt b/doc/source/whatsnew/v0.18.0.txt index 00c7d1d31047f..c749c4540013b 100644 --- a/doc/source/whatsnew/v0.18.0.txt +++ b/doc/source/whatsnew/v0.18.0.txt @@ -615,7 +615,7 @@ other anchored offsets like ``MonthBegin`` and ``YearBegin``. Resample API ^^^^^^^^^^^^ -Like the change in the window functions API :ref:`above `, ``.resample(...)`` is changing to have a more groupby-like API. (:issue:`11732`, :issue:`12702`, :issue:`12202`, :issue:`12332`, :issue:`12348`). +Like the change in the window functions API :ref:`above `, ``.resample(...)`` is changing to have a more groupby-like API. (:issue:`11732`, :issue:`12702`, :issue:`12202`, :issue:`12332`, :issue:`12334`, :issue:`12348`). .. ipython:: python diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index 1b43d3bd76f59..963c6223730f3 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -2550,15 +2550,7 @@ def aggregate(self, func_or_funcs, *args, **kwargs): # _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) + ret = concat(ret, axis=1) return ret agg = aggregate diff --git a/pandas/tests/test_groupby.py b/pandas/tests/test_groupby.py index cc619a998cdd8..2d038f8b16520 100644 --- a/pandas/tests/test_groupby.py +++ b/pandas/tests/test_groupby.py @@ -1532,6 +1532,34 @@ def test_aggregate_api_consistency(self): ['D', 'C']]) assert_frame_equal(result, expected, check_like=True) + def test_agg_compat(self): + + # GH 12334 + + df = DataFrame({'A': ['foo', 'bar', 'foo', 'bar', + 'foo', 'bar', 'foo', 'foo'], + 'B': ['one', 'one', 'two', 'two', + 'two', 'two', 'one', 'two'], + 'C': np.random.randn(8) + 1.0, + 'D': np.arange(8)}) + + g = df.groupby(['A', 'B']) + + expected = pd.concat([g['D'].sum(), + g['D'].std()], + axis=1) + expected.columns = MultiIndex.from_tuples([('C', 'sum'), + ('C', 'std')]) + result = g['D'].agg({'C': ['sum', 'std']}) + assert_frame_equal(result, expected, check_like=True) + + expected = pd.concat([g['D'].sum(), + g['D'].std()], + axis=1) + expected.columns = ['C', 'D'] + result = g['D'].agg({'C': 'sum', 'D': 'std'}) + assert_frame_equal(result, expected, check_like=True) + def test_agg_nested_dicts(self): # API change for disallowing these types of nested dicts diff --git a/pandas/tseries/tests/test_resample.py b/pandas/tseries/tests/test_resample.py index 91922ef5aa9e1..68999ac143ea8 100644 --- a/pandas/tseries/tests/test_resample.py +++ b/pandas/tseries/tests/test_resample.py @@ -424,8 +424,8 @@ def test_agg_misc(self): expected = pd.concat([t['A'].sum(), t['A'].std()], axis=1) - expected.columns = ['sum', 'std'] - + expected.columns = pd.MultiIndex.from_tuples([('A', 'sum'), + ('A', 'std')]) assert_frame_equal(result, expected, check_like=True) expected = pd.concat([t['A'].agg(['sum', 'std']),