Skip to content

BUG: sort_index throws IndexError for some permutations (#26053) #26054

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 1 commit into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.groupby.GroupBy.idxmax` and :meth:`pandas.core.groupby.GroupBy.idxmin` with datetime column would return incorrect dtype (:issue:`25444`, :issue:`15306`)
- Bug in :meth:`pandas.core.groupby.GroupBy.cumsum`, :meth:`pandas.core.groupby.GroupBy.cumprod`, :meth:`pandas.core.groupby.GroupBy.cummin` and :meth:`pandas.core.groupby.GroupBy.cummax` with categorical column having absent categories, would return incorrect result or segfault (:issue:`16771`)


Reshaping
^^^^^^^^^

Expand All @@ -402,6 +403,7 @@ Reshaping
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)
- Bug in :func:`pandas.cut` where large bins could incorrectly raise an error due to an integer overflow (:issue:`26045`)
- Bug in :func:`DataFrame.sort_index` where an error is thrown when a multi-indexed DataFrame is sorted on all levels with the initial level sorted last (:issue:`26053`)
- Bug in :meth:`Series.nlargest` treats ``True`` as smaller than ``False`` (:issue:`26154`)

Sparse
Expand Down
10 changes: 8 additions & 2 deletions pandas/core/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2084,8 +2084,14 @@ def sortlevel(self, level=0, ascending=True, sort_remaining=True):
shape = list(self.levshape)

# partition codes and shape
primary = tuple(codes.pop(lev - i) for i, lev in enumerate(level))
primshp = tuple(shape.pop(lev - i) for i, lev in enumerate(level))
primary = tuple(codes[lev] for lev in level)
primshp = tuple(shape[lev] for lev in level)

# Reverse sorted to retain the order of
# smaller indices that needs to be removed
for lev in sorted(level, reverse=True):
codes.pop(lev)
shape.pop(lev)

if sort_remaining:
primary += primary + tuple(codes)
Expand Down
25 changes: 21 additions & 4 deletions pandas/tests/frame/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,28 @@ def test_sort_index_duplicates(self):
def test_sort_index_level(self):
mi = MultiIndex.from_tuples([[1, 1, 3], [1, 1, 1]], names=list('ABC'))
df = DataFrame([[1, 2], [3, 4]], mi)
res = df.sort_index(level='A', sort_remaining=False)
assert_frame_equal(df, res)

res = df.sort_index(level=['A', 'B'], sort_remaining=False)
assert_frame_equal(df, res)
result = df.sort_index(level='A', sort_remaining=False)
expected = df
assert_frame_equal(result, expected)

result = df.sort_index(level=['A', 'B'], sort_remaining=False)
expected = df
assert_frame_equal(result, expected)

# Error thrown by sort_index when
# first index is sorted last (#26053)
result = df.sort_index(level=['C', 'B', 'A'])
expected = df.iloc[[1, 0]]
assert_frame_equal(result, expected)

result = df.sort_index(level=['B', 'C', 'A'])
expected = df.iloc[[1, 0]]
assert_frame_equal(result, expected)

result = df.sort_index(level=['C', 'A'])
expected = df.iloc[[1, 0]]
assert_frame_equal(result, expected)

def test_sort_index_categorical_index(self):

Expand Down