Skip to content

Commit a3da05a

Browse files
aniaanbeanan
and
beanan
authored
BUG: Index sortlevel ascending add type checking #32334 (#36767)
* BUG: Index sortlevel ascending add type checking #32334 * DOC: add v1.2 whatsnew entry #32334 * BUG: adjust judgment conditions, len(ascending) > 1 => len(ascending) != 1 * DOC: adjustment whatsnew Co-authored-by: beanan <[email protected]>
1 parent 39c6957 commit a3da05a

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ Indexing
353353
- Bug in :meth:`PeriodIndex.get_loc` incorrectly raising ``ValueError`` on non-datelike strings instead of ``KeyError``, causing similar errors in :meth:`Series.__geitem__`, :meth:`Series.__contains__`, and :meth:`Series.loc.__getitem__` (:issue:`34240`)
354354
- Bug in :meth:`Index.sort_values` where, when empty values were passed, the method would break by trying to compare missing values instead of pushing them to the end of the sort order. (:issue:`35584`)
355355
- Bug in :meth:`Index.get_indexer` and :meth:`Index.get_indexer_non_unique` where int64 arrays are returned instead of intp. (:issue:`36359`)
356-
-
356+
- Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`)
357357

358358
Missing
359359
^^^^^^^

pandas/core/indexes/base.py

+14
Original file line numberDiff line numberDiff line change
@@ -1515,6 +1515,20 @@ def sortlevel(self, level=None, ascending=True, sort_remaining=None):
15151515
-------
15161516
Index
15171517
"""
1518+
if not isinstance(ascending, (list, bool)):
1519+
raise TypeError(
1520+
"ascending must be a single bool value or"
1521+
"a list of bool values of length 1"
1522+
)
1523+
1524+
if isinstance(ascending, list):
1525+
if len(ascending) != 1:
1526+
raise TypeError("ascending must be a list of bool values of length 1")
1527+
ascending = ascending[0]
1528+
1529+
if not isinstance(ascending, bool):
1530+
raise TypeError("ascending must be a bool value")
1531+
15181532
return self.sort_values(return_indexer=True, ascending=ascending)
15191533

15201534
def _get_level_values(self, level):

pandas/tests/indexes/test_base.py

+25
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,31 @@ def test_contains_method_removed(self, index):
22222222
with pytest.raises(AttributeError, match=msg):
22232223
index.contains(1)
22242224

2225+
def test_sortlevel(self):
2226+
index = pd.Index([5, 4, 3, 2, 1])
2227+
with pytest.raises(Exception, match="ascending must be a single bool value or"):
2228+
index.sortlevel(ascending="True")
2229+
2230+
with pytest.raises(
2231+
Exception, match="ascending must be a list of bool values of length 1"
2232+
):
2233+
index.sortlevel(ascending=[True, True])
2234+
2235+
with pytest.raises(Exception, match="ascending must be a bool value"):
2236+
index.sortlevel(ascending=["True"])
2237+
2238+
expected = pd.Index([1, 2, 3, 4, 5])
2239+
result = index.sortlevel(ascending=[True])
2240+
tm.assert_index_equal(result[0], expected)
2241+
2242+
expected = pd.Index([1, 2, 3, 4, 5])
2243+
result = index.sortlevel(ascending=True)
2244+
tm.assert_index_equal(result[0], expected)
2245+
2246+
expected = pd.Index([5, 4, 3, 2, 1])
2247+
result = index.sortlevel(ascending=False)
2248+
tm.assert_index_equal(result[0], expected)
2249+
22252250

22262251
class TestMixedIntIndex(Base):
22272252
# Mostly the tests from common.py for which the results differ

0 commit comments

Comments
 (0)