Skip to content

Commit

Permalink
BUG: sort_index throws IndexError for some permutations (pandas-dev#2…
Browse files Browse the repository at this point in the history
…6053)

    - (lev - i) will be out of bounds for pop if the first index dim is to be sorted last
  • Loading branch information
jayanthkaturi committed Apr 21, 2019
1 parent e02ec8f commit 107a575
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ Reshaping
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
- 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:`DataFrame.sort_index` where sorting with the first index level last was throwing an error (:issue:`26053`)

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 @@ -2085,8 +2085,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

0 comments on commit 107a575

Please sign in to comment.