Skip to content

API: consistency in aggregate groupby results #12335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ other anchored offsets like ``MonthBegin`` and ``YearBegin``.
Resample API
^^^^^^^^^^^^

Like the change in the window functions API :ref:`above <whatsnew_0180.enhancements.moments>`, ``.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 <whatsnew_0180.enhancements.moments>`, ``.resample(...)`` is changing to have a more groupby-like API. (:issue:`11732`, :issue:`12702`, :issue:`12202`, :issue:`12332`, :issue:`12334`, :issue:`12348`).

.. ipython:: python

Expand Down
10 changes: 1 addition & 9 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pandas/tseries/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
Expand Down