Skip to content

Commit 286d304

Browse files
committed
API: consistency in aggregate groupby results
closes #12334 Author: Jeff Reback <[email protected]> Closes #12335 from jreback/groupby2 and squashes the following commits: 522dd79 [Jeff Reback] API: consistency in aggregate groupby results
1 parent fc321fb commit 286d304

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

doc/source/whatsnew/v0.18.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ other anchored offsets like ``MonthBegin`` and ``YearBegin``.
615615
Resample API
616616
^^^^^^^^^^^^
617617

618-
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`).
618+
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`).
619619

620620
.. ipython:: python
621621

pandas/core/groupby.py

+1-9
Original file line numberDiff line numberDiff line change
@@ -2550,15 +2550,7 @@ def aggregate(self, func_or_funcs, *args, **kwargs):
25502550
# _level handled at higher
25512551
if not _level and isinstance(ret, dict):
25522552
from pandas import concat
2553-
2554-
# our result is a Series-like
2555-
if len(ret) == 1:
2556-
ret = concat([r for r in ret.values()],
2557-
axis=1)
2558-
2559-
# our result is a DataFrame like
2560-
else:
2561-
ret = concat(ret, axis=1)
2553+
ret = concat(ret, axis=1)
25622554
return ret
25632555

25642556
agg = aggregate

pandas/tests/test_groupby.py

+28
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,34 @@ def test_aggregate_api_consistency(self):
15321532
['D', 'C']])
15331533
assert_frame_equal(result, expected, check_like=True)
15341534

1535+
def test_agg_compat(self):
1536+
1537+
# GH 12334
1538+
1539+
df = DataFrame({'A': ['foo', 'bar', 'foo', 'bar',
1540+
'foo', 'bar', 'foo', 'foo'],
1541+
'B': ['one', 'one', 'two', 'two',
1542+
'two', 'two', 'one', 'two'],
1543+
'C': np.random.randn(8) + 1.0,
1544+
'D': np.arange(8)})
1545+
1546+
g = df.groupby(['A', 'B'])
1547+
1548+
expected = pd.concat([g['D'].sum(),
1549+
g['D'].std()],
1550+
axis=1)
1551+
expected.columns = MultiIndex.from_tuples([('C', 'sum'),
1552+
('C', 'std')])
1553+
result = g['D'].agg({'C': ['sum', 'std']})
1554+
assert_frame_equal(result, expected, check_like=True)
1555+
1556+
expected = pd.concat([g['D'].sum(),
1557+
g['D'].std()],
1558+
axis=1)
1559+
expected.columns = ['C', 'D']
1560+
result = g['D'].agg({'C': 'sum', 'D': 'std'})
1561+
assert_frame_equal(result, expected, check_like=True)
1562+
15351563
def test_agg_nested_dicts(self):
15361564

15371565
# API change for disallowing these types of nested dicts

pandas/tseries/tests/test_resample.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ def test_agg_misc(self):
424424
expected = pd.concat([t['A'].sum(),
425425
t['A'].std()],
426426
axis=1)
427-
expected.columns = ['sum', 'std']
428-
427+
expected.columns = pd.MultiIndex.from_tuples([('A', 'sum'),
428+
('A', 'std')])
429429
assert_frame_equal(result, expected, check_like=True)
430430

431431
expected = pd.concat([t['A'].agg(['sum', 'std']),

0 commit comments

Comments
 (0)