Skip to content

Pass sort for agg multiple #21062

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 2 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 pandas/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean with "Still sorts the index for backwards compat"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though we pass .agg(['sum', 'min']) the result's index is the sorted function names, ['min', 'sum']. Make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could imaging preserving the function order here someday, but would need to deprecate first.

Copy link
Contributor Author

@TomAugspurger TomAugspurger May 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can update the comment to say "Still sorts the result's index"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok, the sorting is about the [sum, min] axis, then I understand

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})
Expand Down