Skip to content

Commit db6993c

Browse files
alexander-ponomaroffWillAyd
authored andcommitted
Replace dicts with OrderedDicts in groupby aggregation functions (pandas-dev#25693)
1 parent 5bdb0ac commit db6993c

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ Groupby/Resample/Rolling
253253
- Bug in :meth:`pandas.core.groupby.DataFrameGroupBy.nunique` in which the names of column levels were lost (:issue:`23222`)
254254
- Bug in :func:`pandas.core.groupby.GroupBy.agg` when applying a aggregation function to timezone aware data (:issue:`23683`)
255255
- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` where timezone information would be dropped (:issue:`21603`)
256+
- Ensured that ordering of outputs in ``groupby`` aggregation functions is consistent across all versions of Python (:issue:`25692`)
256257

257258

258259
Reshaping

pandas/core/groupby/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def _aggregate_generic(self, func, *args, **kwargs):
219219
axis = self.axis
220220
obj = self._obj_with_exclusions
221221

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

248248
obj = self._obj_with_exclusions
249-
result = {}
249+
result = collections.OrderedDict()
250250
cannot_agg = []
251251
errors = None
252252
for item in obj:
@@ -899,7 +899,7 @@ def _get_index():
899899
name=self._selection_name)
900900

901901
def _aggregate_named(self, func, *args, **kwargs):
902-
result = {}
902+
result = collections.OrderedDict()
903903

904904
for name, group in self:
905905
group.name = name

pandas/tests/groupby/aggregate/test_aggregate.py

+12
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,15 @@ def test_groupby_agg_coercing_bools():
303303
result = gp['c'].aggregate(lambda x: x.isnull().all())
304304
expected = Series([True, False], index=index, name='c')
305305
tm.assert_series_equal(result, expected)
306+
307+
308+
def test_order_aggregate_multiple_funcs():
309+
# GH 25692
310+
df = pd.DataFrame({'A': [1, 1, 2, 2], 'B': [1, 2, 3, 4]})
311+
312+
res = df.groupby('A').agg(['sum', 'max', 'mean', 'ohlc', 'min'])
313+
result = res.columns.levels[1]
314+
315+
expected = pd.Index(['sum', 'max', 'mean', 'ohlc', 'min'])
316+
317+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)