From 121cd0e2aa0bcb9b6a548f3dff0fbba7ef2b26ea Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 15 May 2018 11:07:06 -0500 Subject: [PATCH 1/2] Pass sort for agg multiple --- pandas/core/base.py | 2 +- pandas/tests/frame/test_apply.py | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pandas/core/base.py b/pandas/core/base.py index 5022beabef76b..fa78c89ed4ee7 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -608,7 +608,7 @@ def _aggregate_multiple_funcs(self, arg, _level, _axis): raise ValueError("no results") try: - return concat(results, keys=keys, axis=1) + return concat(results, keys=keys, axis=1, sort=False) except TypeError: # we are concatting non-NDFrame objects, diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index af39c8f01cf73..92068191e12ea 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -908,6 +908,29 @@ def test_demo(self): index=['max', 'min', 'sum']) tm.assert_frame_equal(result.reindex_like(expected), expected) + def test_agg_multiple_mixed_no_warning(self): + # https://github.com/pandas-dev/pandas/issues/20909 + mdf = pd.DataFrame({'A': [1, 2, 3], + 'B': [1., 2., 3.], + 'C': ['foo', 'bar', 'baz'], + 'D': pd.date_range('20130101', periods=3)}) + expected = pd.DataFrame({"A": [1, 6], 'B': [1.0, 6.0], + "C": ['bar', 'foobarbaz'], + "D": [pd.Timestamp('2013-01-01'), pd.NaT]}, + index=['min', 'sum']) + # sorted index + with tm.assert_produces_warning(None): + result = mdf.agg(['min', 'sum']) + + tm.assert_frame_equal(result, expected) + + # unsorted index. Still sorts the index for backwards compat + with tm.assert_produces_warning(None): + result = mdf[['D', 'C', 'B', 'A']].agg(['sum', 'min']) + + expected = expected[['D', 'C', 'B', 'A']] + tm.assert_frame_equal(result, expected) + def test_agg_dict_nested_renaming_depr(self): df = pd.DataFrame({'A': range(5), 'B': 5}) From ac0d1f575b9541f8cc5950904edf6068822c31a1 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 15 May 2018 15:01:27 -0500 Subject: [PATCH 2/2] Clarify comment [ci skip] [ci skip] --- pandas/tests/frame/test_apply.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_apply.py b/pandas/tests/frame/test_apply.py index 92068191e12ea..ac46f02d00773 100644 --- a/pandas/tests/frame/test_apply.py +++ b/pandas/tests/frame/test_apply.py @@ -924,10 +924,12 @@ def test_agg_multiple_mixed_no_warning(self): tm.assert_frame_equal(result, expected) - # unsorted index. Still sorts the index for backwards compat with tm.assert_produces_warning(None): result = mdf[['D', 'C', 'B', 'A']].agg(['sum', 'min']) + # For backwards compatibility, the result's index is + # still sorted by function name, so it's ['min', 'sum'] + # not ['sum', 'min']. expected = expected[['D', 'C', 'B', 'A']] tm.assert_frame_equal(result, expected)