-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: MultiIndex sort with ascending as list #16937
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,6 +156,7 @@ Indexing | |
- When called on an unsorted ``MultiIndex``, the ``loc`` indexer now will raise ``UnsortedIndexError`` only if proper slicing is used on non-sorted levels (:issue:`16734`). | ||
- Fixes regression in 0.20.3 when indexing with a string on a ``TimedeltaIndex`` (:issue:`16896`). | ||
- Fixed ``TimedeltaIndex.get_loc`` handling of ``np.timedelta64`` inputs (:issue:`16909`). | ||
- Fix MultiIndex ``sort_index`` ordering when ``ascending`` argument is a list but not all levels are specified, or are in a different order (:issue:`16934`). | ||
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.
should link to the method. |
||
|
||
I/O | ||
^^^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2781,3 +2781,20 @@ def test_sort_index_nan(self): | |
result = s.sort_index(na_position='first') | ||
expected = s.iloc[[1, 2, 3, 0]] | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_sort_ascending_list(self): | ||
# GH: 16934 | ||
|
||
# Set up a Series with a three level MultiIndex | ||
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], | ||
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two'], | ||
[4, 3, 2, 1, 4, 3, 2, 1]] | ||
tuples = list(zip(*arrays)) | ||
index = pd.MultiIndex.from_tuples(tuples, | ||
names=['first', 'second', 'third']) | ||
s = pd.Series(range(8), index=index) | ||
|
||
result = s.sort_index(level=['third', 'first'], | ||
ascending=[False, True]) | ||
|
||
assert np.array_equal(result, [0, 4, 1, 5, 2, 6, 3, 7]) | ||
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. As a general rule of thumb, we try to avoid using Secondly, you should be constructing a 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. can you also both cases from the original issue (in same tests) IOW assert first one works as well
|
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.
Nit: add a comma before the "...but not all levels"
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.
Also, I might reword the beginning as follows: