|
11 | 11 | from pandas.core.index import Index, MultiIndex |
12 | 12 | from pandas import Panel, DataFrame, Series, notnull, isnull, Timestamp |
13 | 13 |
|
| 14 | +from pandas.core.common import UnsortedIndexError |
14 | 15 | from pandas.types.common import is_float_dtype, is_integer_dtype |
15 | 16 | import pandas.core.common as com |
16 | 17 | import pandas.util.testing as tm |
@@ -2612,3 +2613,23 @@ def my_func(group): |
2612 | 2613 | names=['letter', 'size', None]) |
2613 | 2614 |
|
2614 | 2615 | tm.assert_index_equal(result.index, expected) |
| 2616 | + |
| 2617 | + def test_sort_non_lexsorted(self): |
| 2618 | + # degenerate case where we sort but don't |
| 2619 | + # have a satisfying result :< |
| 2620 | + |
| 2621 | + idx = MultiIndex([['A', 'B', 'C'], |
| 2622 | + ['c', 'b', 'a']], |
| 2623 | + [[0, 1, 2, 0, 1, 2], |
| 2624 | + [0, 2, 1, 1, 0, 2]]) |
| 2625 | + |
| 2626 | + df = DataFrame({'col': range(len(idx))}, index=idx) |
| 2627 | + assert df.index.is_lexsorted() is False |
| 2628 | + assert df.index.is_monotonic is False |
| 2629 | + |
| 2630 | + result = df.sort_index() |
| 2631 | + assert result.index.is_lexsorted() is False |
| 2632 | + assert result.index.is_monotonic is True |
| 2633 | + |
| 2634 | + with pytest.raises(UnsortedIndexError): |
| 2635 | + result.loc[pd.IndexSlice['B':'C', 'a':'c'], :] |
0 commit comments