-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: raise when sort_index with ascending=None #39451
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
Changes from 4 commits
4f34ed1
8e40cac
513e0c5
dd2d191
f06d5f4
4b8da49
8363293
29dd29a
25b4119
6a422b2
860c8a5
a51a343
e0ebf58
6b430f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4533,6 +4533,8 @@ def sort_index( | |
|
|
||
| inplace = validate_bool_kwarg(inplace, "inplace") | ||
| axis = self._get_axis_number(axis) | ||
| ascending = validate_ascending(ascending) | ||
|
|
||
| target = self._get_axis(axis) | ||
|
|
||
| indexer = get_indexer_indexer( | ||
|
|
@@ -11772,3 +11774,12 @@ def _doc_params(cls): | |
| The required number of valid values to perform the operation. If fewer than | ||
| ``min_count`` non-NA values are present the result will be NA. | ||
| """ | ||
|
|
||
|
|
||
| def validate_ascending(ascending): | ||
|
||
| """Validate ``ascending`` kwargs for ``sort_index`` method.""" | ||
| kwargs = {"none_allowed": False, "int_allowed": True} | ||
| if not isinstance(ascending, (list, tuple)): | ||
| return validate_bool_kwarg(ascending, "ascending", **kwargs) | ||
|
|
||
| return [validate_bool_kwarg(item, "ascending", **kwargs) for item in ascending] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -761,6 +761,22 @@ def test_sort_index_with_categories(self, categories): | |
| ) | ||
| tm.assert_frame_equal(result, expected) | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "ascending", | ||
| [ | ||
| None, | ||
| (True, None), | ||
| (False, "True"), | ||
|
||
| ], | ||
| ) | ||
| def test_sort_index_ascending_bad_value_raises(self, ascending): | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| df = DataFrame(np.arange(64)) | ||
| length = len(df.index) | ||
| df.index = [(i - length / 2) % length for i in range(length)] | ||
| match = 'For argument "ascending" expected type bool' | ||
| with pytest.raises(ValueError, match=match): | ||
| df.sort_index(axis=0, ascending=ascending, na_position="first") | ||
|
|
||
|
|
||
| class TestDataFrameSortIndexKey: | ||
| def test_sort_multi_index_key(self): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -205,9 +205,39 @@ def validate_args_and_kwargs(fname, args, kwargs, max_fname_arg_count, compat_ar | |
| validate_kwargs(fname, kwargs, compat_args) | ||
|
|
||
|
|
||
| def validate_bool_kwarg(value, arg_name): | ||
| """ Ensures that argument passed in arg_name is of type bool. """ | ||
| if not (is_bool(value) or value is None): | ||
| def validate_bool_kwarg(value, arg_name, none_allowed=True, int_allowed=False): | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Ensure that argument passed in arg_name can be interpreted as boolean. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| value : bool | ||
| Value to be validated. | ||
| arg_name : str | ||
| Name of the argument. To be reflected in the error message. | ||
| none_allowed : bool, optional | ||
|
||
| Whether to consider None to be a valid boolean. | ||
| int_allowed : bool, optional | ||
|
||
| Whether to consider integer value to be a valid boolean. | ||
|
|
||
| Returns | ||
| ------- | ||
| value | ||
| The same value as input. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If the value is not a valid boolean. | ||
| """ | ||
| good_value = is_bool(value) | ||
| if none_allowed: | ||
| good_value = good_value or value is None | ||
|
|
||
| if int_allowed: | ||
| good_value = good_value or isinstance(value, int) | ||
|
|
||
| if not good_value: | ||
| raise ValueError( | ||
| f'For argument "{arg_name}" expected type bool, received ' | ||
| f"type {type(value).__name__}." | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.