Skip to content

Replace dicts with OrderedDicts in groupby aggregation functions #25693

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

Merged
merged 7 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
- Bug in :func:`pandas.core.groupby.GroupBy.agg` when applying a aggregation function to timezone aware data (:issue:`23683`)
- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` where timezone information would be dropped (:issue:`21603`)
- Replaced regular dictionaries with ``OrderedDict`` in the aggregation functions of ``groupby`` to make the ordering consistent in all versions of Python (:issue:`25692`)


Reshaping
Expand Down
6 changes: 3 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def _aggregate_generic(self, func, *args, **kwargs):
axis = self.axis
obj = self._obj_with_exclusions

result = {}
result = collections.OrderedDict()
if axis != obj._info_axis_number:
try:
for name, data in self:
Expand All @@ -246,7 +246,7 @@ def _aggregate_item_by_item(self, func, *args, **kwargs):
# only for axis==0

obj = self._obj_with_exclusions
result = {}
result = collections.OrderedDict()
cannot_agg = []
errors = None
for item in obj:
Expand Down Expand Up @@ -899,7 +899,7 @@ def _get_index():
name=self._selection_name)

def _aggregate_named(self, func, *args, **kwargs):
result = {}
result = collections.OrderedDict()

for name, group in self:
group.name = name
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/aggregate/test_aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,15 @@ def test_groupby_agg_coercing_bools():
result = gp['c'].aggregate(lambda x: x.isnull().all())
expected = Series([True, False], index=index, name='c')
tm.assert_series_equal(result, expected)


def test_order_aggregate_multiple_funcs():
# GH 25692
df = pd.DataFrame({'A': [1, 1, 2, 2], 'B': [1, 2, 3, 4]})

res = df.groupby('A').agg(['sum', 'max', 'mean', 'ohlc', 'min'])
result = res.columns.levels[1]

expected = pd.Index(['sum', 'max', 'mean', 'ohlc', 'min'])

tm.assert_index_equal(result, expected)