Skip to content
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

BUG: Index sortlevel ascending add type checking #32334 #36767

Merged
merged 4 commits into from
Oct 1, 2020
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: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ Indexing
- 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`)
- 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`)
- Bug in :meth:`Index.get_indexer` and :meth:`Index.get_indexer_non_unique` where int64 arrays are returned instead of intp. (:issue:`36359`)
-
- Bug in :meth:`DataFrame.sort_index` where parameter ascending passed as a list on a single level index gives wrong result. (:issue:`32334`)

Missing
^^^^^^^
Expand Down
14 changes: 14 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,20 @@ def sortlevel(self, level=None, ascending=True, sort_remaining=None):
-------
Index
"""
if not isinstance(ascending, (list, bool)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these should be checked at a much lower level, where they are used; the reason these are fairly generic things not specific to Index in this case

raise TypeError(
"ascending must be a single bool value or"
"a list of bool values of length 1"
)

if isinstance(ascending, list):
if len(ascending) != 1:
raise TypeError("ascending must be a list of bool values of length 1")
ascending = ascending[0]

if not isinstance(ascending, bool):
raise TypeError("ascending must be a bool value")

return self.sort_values(return_indexer=True, ascending=ascending)

def _get_level_values(self, level):
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2222,6 +2222,31 @@ def test_contains_method_removed(self, index):
with pytest.raises(AttributeError, match=msg):
index.contains(1)

def test_sortlevel(self):
index = pd.Index([5, 4, 3, 2, 1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we generally add the issue number here as a comment

with pytest.raises(Exception, match="ascending must be a single bool value or"):
index.sortlevel(ascending="True")

with pytest.raises(
Exception, match="ascending must be a list of bool values of length 1"
):
index.sortlevel(ascending=[True, True])

with pytest.raises(Exception, match="ascending must be a bool value"):
index.sortlevel(ascending=["True"])

expected = pd.Index([1, 2, 3, 4, 5])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be 2 tests, one for the exceptions, one for the working cases (which could be parameterized)

result = index.sortlevel(ascending=[True])
tm.assert_index_equal(result[0], expected)

expected = pd.Index([1, 2, 3, 4, 5])
result = index.sortlevel(ascending=True)
tm.assert_index_equal(result[0], expected)

expected = pd.Index([5, 4, 3, 2, 1])
result = index.sortlevel(ascending=False)
tm.assert_index_equal(result[0], expected)


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