Skip to content

Commit dd2d191

Browse files
committed
REF: add kwargs none_allowed and int_allowed
1 parent 513e0c5 commit dd2d191

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

pandas/core/generic.py

+4-14
Original file line numberDiff line numberDiff line change
@@ -11776,20 +11776,10 @@ def _doc_params(cls):
1177611776
"""
1177711777

1177811778

11779-
def validate_ascending(
11780-
ascending: Union[bool, Sequence[bool]],
11781-
) -> Union[bool, Sequence[bool]]:
11779+
def validate_ascending(ascending):
1178211780
"""Validate ``ascending`` kwargs for ``sort_index`` method."""
11781+
kwargs = {"none_allowed": False, "int_allowed": True}
1178311782
if not isinstance(ascending, (list, tuple)):
11784-
_check_ascending_element(ascending)
11785-
return ascending
11783+
return validate_bool_kwarg(ascending, "ascending", **kwargs)
1178611784

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 value is None or not isinstance(value, (bool, int)):
11795-
raise ValueError("ascending must be either a bool or a sequence of bools")
11785+
return [validate_bool_kwarg(item, "ascending", **kwargs) for item in ascending]

pandas/tests/frame/methods/test_sort_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ def test_sort_index_ascending_bad_value_raises(self, ascending):
773773
df = DataFrame(np.arange(64))
774774
length = len(df.index)
775775
df.index = [(i - length / 2) % length for i in range(length)]
776-
match = "ascending must be either a bool or a sequence of bools"
776+
match = 'For argument "ascending" expected type bool'
777777
with pytest.raises(ValueError, match=match):
778778
df.sort_index(axis=0, ascending=ascending, na_position="first")
779779

pandas/tests/series/methods/test_sort_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ def test_sort_index_ascending_list(self):
208208
)
209209
def test_sort_index_ascending_bad_value_raises(self, ascending):
210210
ser = Series(range(10), index=[0, 3, 2, 1, 4, 5, 7, 6, 8, 9])
211-
match = "ascending must be either a bool or a sequence of bools"
211+
match = 'For argument "ascending" expected type bool'
212212
with pytest.raises(ValueError, match=match):
213213
ser.sort_index(ascending=ascending)
214214

pandas/util/_validators.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,39 @@ def validate_args_and_kwargs(fname, args, kwargs, max_fname_arg_count, compat_ar
205205
validate_kwargs(fname, kwargs, compat_args)
206206

207207

208-
def validate_bool_kwarg(value, arg_name):
209-
""" Ensures that argument passed in arg_name is of type bool. """
210-
if not (is_bool(value) or value is None):
208+
def validate_bool_kwarg(value, arg_name, none_allowed=True, int_allowed=False):
209+
"""
210+
Ensure that argument passed in arg_name can be interpreted as boolean.
211+
212+
Parameters
213+
----------
214+
value : bool
215+
Value to be validated.
216+
arg_name : str
217+
Name of the argument. To be reflected in the error message.
218+
none_allowed : bool, optional
219+
Whether to consider None to be a valid boolean.
220+
int_allowed : bool, optional
221+
Whether to consider integer value to be a valid boolean.
222+
223+
Returns
224+
-------
225+
value
226+
The same value as input.
227+
228+
Raises
229+
------
230+
ValueError
231+
If the value is not a valid boolean.
232+
"""
233+
good_value = is_bool(value)
234+
if none_allowed:
235+
good_value = good_value or value is None
236+
237+
if int_allowed:
238+
good_value = good_value or isinstance(value, int)
239+
240+
if not good_value:
211241
raise ValueError(
212242
f'For argument "{arg_name}" expected type bool, received '
213243
f"type {type(value).__name__}."

0 commit comments

Comments
 (0)