8000 BUG: DataFrame.sort_index broken if not both lexsorted and monotonic in levels by jreback · Pull Request #15694 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

BUG: DataFrame.sort_index broken if not both lexsorted and monotonic in levels #15694

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

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
add degenerate test case
  • Loading branch information
jreback committed Apr 7, 2017
commit 3c4ca22fa5bd190d00f79867d4f99d712da97f93
21 changes: 21 additions & 0 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pandas.core.index import Index, MultiIndex
from pandas import Panel, DataFrame, Series, notnull, isnull, Timestamp

from pandas.core.common import UnsortedIndexError
from pandas.types.common import is_float_dtype, is_integer_dtype
import pandas.core.common as com
import pandas.util.testing as tm
Expand Down Expand Up @@ -2612,3 +2613,23 @@ def my_func(group):
names=['letter', 'size', None])

tm.assert_index_equal(result.index, expected)

def test_sort_non_lexsorted(self):
# degenerate case where we sort but don't
# have a satisfying result :<

idx = MultiIndex([['A', 'B', 'C'],
['c', 'b', 'a']],
[[0, 1, 2, 0, 1, 2],
[0, 2, 1, 1, 0, 2]])

df = DataFrame({'col': range(len(idx))}, index=idx)
assert df.index.is_lexsorted() is False
assert df.index.is_monotonic is False

result = df.sort_index()
assert result.index.is_lexsorted() is False
assert result.index.is_monotonic is True

with pytest.raises(UnsortedIndexError):
result.loc[pd.IndexSlice['B':'C', 'a':'c'], :]
0