Skip to content

Commit 8e40cac

Browse files
committed
BUG: add ascending kwarg validation
1 parent 4f34ed1 commit 8e40cac

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

pandas/core/generic.py

+24
Original file line numberDiff line numberDiff line change
@@ -4533,6 +4533,8 @@ def sort_index(
45334533

45344534
inplace = validate_bool_kwarg(inplace, "inplace")
45354535
axis = self._get_axis_number(axis)
4536+
ascending = validate_ascending(ascending)
4537+
45364538
target = self._get_axis(axis)
45374539

45384540
indexer = get_indexer_indexer(
@@ -11772,3 +11774,25 @@ def _doc_params(cls):
1177211774
The required number of valid values to perform the operation. If fewer than
1177311775
``min_count`` non-NA values are present the result will be NA.
1177411776
"""
11777+
11778+
11779+
def validate_ascending(
11780+
ascending: Union[bool, Sequence[bool]],
11781+
) -> Union[bool, Sequence[bool]]:
11782+
"""Validate ``ascending`` kwargs for ``sort_index`` method."""
11783+
if not isinstance(ascending, (list, tuple)):
11784+
_check_ascending_element(ascending)
11785+
return ascending
11786+
11787+
for item in ascending:
11788+
_check_ascending_element(item)
11789+
return ascending
11790+
11791+
11792+
def _check_ascending_element(value):
11793+
"""Ensure that each item in ``ascending`` kwarg is either bool or int."""
11794+
if (
11795+
value is None
11796+
or not isinstance(value, (bool, int))
11797+
):
11798+
raise ValueError("ascending must be either a bool or a sequence of bools")

0 commit comments

Comments
 (0)