Skip to content

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
merged 4 commits into from
Oct 1, 2017
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
Prev Previous commit
DOC: Add the description for fix unexpected sort in groupby in whatsn…
…ew note
  • Loading branch information
Licht-T committed Sep 29, 2017
commit 9b6a3da7cb60d32c1c13698d06b0228deaa1d92e
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ Groupby/Resample/Rolling
- Bug in ``.rolling(...).apply(...)`` with a ``DataFrame`` with a ``DatetimeIndex``, a ``window`` of a timedelta-convertible and ``min_periods >= 1` (:issue:`15305`)
- Bug in ``DataFrame.groupby`` where index and column keys were not recognized correctly when the number of keys equaled the number of elements on the groupby axis (:issue:`16859`)
- Bug in ``groupby.nunique()`` with ``TimeGrouper`` which cannot handle ``NaT`` correctly (:issue:`17575`)
- Bug in ``DataFrame.groupby`` where a single level selection from a ``MultiIndex`` unexpectedly sorts (:issue:`17537`)

Sparse
^^^^^^
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,7 @@ def aggfun(ser):

@pytest.mark.parametrize('sort', [True, False])
def test_groupby_level(self, sort):
# GH 17537
frame = self.mframe
Copy link
Contributor

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

deleveled = frame.reset_index()

Expand Down Expand Up @@ -1838,6 +1839,7 @@ def test_groupby_level_index_names(self):

@pytest.mark.parametrize('sort', [True, False])
def test_groupby_level_with_nas(self, sort):
# GH 17537
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]])
Expand Down Expand Up @@ -1943,6 +1945,7 @@ def test_groupby_complex(self):
[False, [0, 0, 0, 1, 1, 2, 2, 3, 3, 3]]
])
def test_level_preserve_order(self, sort, labels):
# GH 17537
grouped = self.mframe.groupby(level=0, sort=sort)
exp_labels = np.array(labels, np.intp)
assert_almost_equal(grouped.grouper.labels[0], exp_labels)
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/groupby/test_whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ def test_regression_whitelist_methods(
raw_frame, op, level,
axis, skipna, sort):
# GH6944
# GH 17537
# explicity test the whitelest methods

if axis == 0:
Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ def test_count(self):

@pytest.mark.parametrize('sort', [True, False])
def test_series_group_min_max(self, sort):
# GH 17537
for op, level, skipna in cart_product(self.AGG_FUNCTIONS, lrange(2),
[False, True]):
grouped = self.series.groupby(level=level, sort=sort)
Expand All @@ -1407,6 +1408,7 @@ def test_series_group_min_max(self, sort):

@pytest.mark.parametrize('sort', [True, False])
def test_frame_group_ops(self, sort):
# GH 17537
self.frame.iloc[1, [1, 2]] = np.nan
self.frame.iloc[7, [0, 1]] = np.nan

Expand Down