-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
BUG: Fix unexpected sort in groupby #17621
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
jreback merged 4 commits into pandas-dev:master from Licht-T:fix-unexpected-sort-groupby Oct 1, 2017
Merged
Changes from 1 commit
Commits
Show all changes
4 commits Select commit Hold shift + click to select a range
0e7bdb3 BUG: Fix unexpected sort behavior when single level groupby from Mult…
Licht-T c3a1701 BUG: Fix unexpected sort behavior on aggregation
Licht-T 7b23e65 TST: Fix existing tests for groupby
Licht-T 9b6a3da DOC: Add the description for fix unexpected sort in groupby in whatsn…
Licht-T File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
TST: Fix existing tests for groupby
- Loading branch information
commit 7b23e657fd40330fb166827326da5cf54601ea0b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| | @@ -1791,18 +1791,19 @@ def aggfun(ser): | |
| agged2 = df.groupby(keys).aggregate(aggfun) | ||
| assert len(agged2.columns) + 1 == len(df.columns) | ||
| | ||
| def test_groupby_level(self): | ||
| @pytest.mark.parametrize('sort', [True, False]) | ||
| def test_groupby_level(self, sort): | ||
| frame = self.mframe | ||
| deleveled = frame.reset_index() | ||
| | ||
| result0 = frame.groupby(level=0).sum() | ||
| result1 = frame.groupby(level=1).sum() | ||
| result0 = frame.groupby(level=0, sort=sort).sum() | ||
| result1 = frame.groupby(level=1, sort=sort).sum() | ||
| | ||
| expected0 = frame.groupby(deleveled['first'].values).sum() | ||
| expected1 = frame.groupby(deleveled['second'].values).sum() | ||
| expected0 = frame.groupby(deleveled['first'].values, sort=sort).sum() | ||
| expected1 = frame.groupby(deleveled['second'].values, sort=sort).sum() | ||
| | ||
| expected0 = expected0.reindex(frame.index.levels[0]) | ||
| expected1 = expected1.reindex(frame.index.levels[1]) | ||
| expected0.index.name = 'first' | ||
| expected1.index.name = 'second' | ||
| | ||
| assert result0.index.name == 'first' | ||
| assert result1.index.name == 'second' | ||
| | @@ -1813,15 +1814,15 @@ def test_groupby_level(self): | |
| assert result1.index.name == frame.index.names[1] | ||
| | ||
| # groupby level name | ||
| result0 = frame.groupby(level='first').sum() | ||
| result1 = frame.groupby(level='second').sum() | ||
| result0 = frame.groupby(level='first', sort=sort).sum() | ||
| result1 = frame.groupby(level='second', sort=sort).sum() | ||
| assert_frame_equal(result0, expected0) | ||
| assert_frame_equal(result1, expected1) | ||
| | ||
| # axis=1 | ||
| | ||
| result0 = frame.T.groupby(level=0, axis=1).sum() | ||
| result1 = frame.T.groupby(level=1, axis=1).sum() | ||
| Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for a couple of these that you changed can you also add the sort=True case (maybe parametrize on sort=)? | ||
| result0 = frame.T.groupby(level=0, axis=1, sort=sort).sum() | ||
| result1 = frame.T.groupby(level=1, axis=1, sort=sort).sum() | ||
| assert_frame_equal(result0, expected0.T) | ||
| assert_frame_equal(result1, expected1.T) | ||
| | ||
| | @@ -1835,15 +1836,16 @@ def test_groupby_level_index_names(self): | |
| df.groupby(level='exp') | ||
| pytest.raises(ValueError, df.groupby, level='foo') | ||
| | ||
| def test_groupby_level_with_nas(self): | ||
| @pytest.mark.parametrize('sort', [True, False]) | ||
| def test_groupby_level_with_nas(self, sort): | ||
| index = MultiIndex(levels=[[1, 0], [0, 1, 2, 3]], | ||
| labels=[[1, 1, 1, 1, 0, 0, 0, 0], [0, 1, 2, 3, 0, 1, | ||
| 2, 3]]) | ||
| | ||
| # factorizing doesn't confuse things | ||
| s = Series(np.arange(8.), index=index) | ||
| result = s.groupby(level=0).sum() | ||
| expected = Series([22., 6.], index=[1, 0]) | ||
| result = s.groupby(level=0, sort=sort).sum() | ||
| expected = Series([6., 22.], index=[0, 1]) | ||
| assert_series_equal(result, expected) | ||
| | ||
| index = MultiIndex(levels=[[1, 0], [0, 1, 2, 3]], | ||
| | @@ -1852,8 +1854,8 @@ def test_groupby_level_with_nas(self): | |
| | ||
| # factorizing doesn't confuse things | ||
| s = Series(np.arange(8.), index=index) | ||
| result = s.groupby(level=0).sum() | ||
| expected = Series([18., 6.], index=[1, 0]) | ||
| result = s.groupby(level=0, sort=sort).sum() | ||
| expected = Series([6., 18.], index=[0.0, 1.0]) | ||
| assert_series_equal(result, expected) | ||
| | ||
| def test_groupby_level_apply(self): | ||
| | @@ -1936,9 +1938,13 @@ def test_groupby_complex(self): | |
| result = a.sum(level=0) | ||
| assert_series_equal(result, expected) | ||
| | ||
| def test_level_preserve_order(self): | ||
| grouped = self.mframe.groupby(level=0) | ||
| exp_labels = np.array([0, 0, 0, 1, 1, 2, 2, 3, 3, 3], np.intp) | ||
| @pytest.mark.parametrize('sort,labels', [ | ||
| [True, [2, 2, 2, 0, 0, 1, 1, 3, 3, 3]], | ||
| [False, [0, 0, 0, 1, 1, 2, 2, 3, 3, 3]] | ||
| ]) | ||
| def test_level_preserve_order(self, sort, labels): | ||
| grouped = self.mframe.groupby(level=0, sort=sort) | ||
| exp_labels = np.array(labels, np.intp) | ||
| assert_almost_equal(grouped.grouper.labels[0], exp_labels) | ||
| | ||
| def test_grouping_labels(self): | ||
| | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add the issue number here as well as a comment