Skip to content

Raise if None is passed for skipna #44579

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

Merged
merged 3 commits into from
Nov 24, 2021
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ Other API changes
^^^^^^^^^^^^^^^^^
- :meth:`Index.get_indexer_for` no longer accepts keyword arguments (other than 'target'); in the past these would be silently ignored if the index was not unique (:issue:`42310`)
- Change in the position of the ``min_rows`` argument in :meth:`DataFrame.to_string` due to change in the docstring (:issue:`44304`)
- Reduction operations for :class:`DataFrame` or :class:`Series` now raising a ``ValueError`` when ``None`` is passed for ``skipna`` (:issue:`44178`)
-

.. ---------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10303,6 +10303,7 @@ def _logical_func(
self, name: str, func, axis=0, bool_only=None, skipna=True, level=None, **kwargs
):
nv.validate_logical_func((), kwargs, fname=name)
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
if level is not None:
warnings.warn(
"Using the level keyword in DataFrame and Series aggregations is "
Expand Down Expand Up @@ -10397,6 +10398,7 @@ def _stat_function_ddof(
**kwargs,
):
nv.validate_stat_ddof_func((), kwargs, fname=name)
validate_bool_kwarg(skipna, "skipna", none_allowed=False)
if axis is None:
axis = self._stat_axis_number
if level is not None:
Expand Down Expand Up @@ -10450,6 +10452,9 @@ def _stat_function(
nv.validate_median((), kwargs)
else:
nv.validate_stat_func((), kwargs, fname=name)

validate_bool_kwarg(skipna, "skipna", none_allowed=False)

if axis is None:
axis = self._stat_axis_number
if level is not None:
Expand Down Expand Up @@ -10517,6 +10522,9 @@ def _min_count_stat_function(
nv.validate_prod((), kwargs)
else:
nv.validate_stat_func((), kwargs, fname=name)

validate_bool_kwarg(skipna, "skipna", none_allowed=False)

if axis is None:
axis = self._stat_axis_number
if level is not None:
Expand Down Expand Up @@ -10669,7 +10677,7 @@ def all(self, axis=0, bool_only=None, skipna=True, level=None, **kwargs):
see_also="",
examples="",
)
def mad(self, axis=None, skipna=None, level=None):
def mad(self, axis=None, skipna=True, level=None):
return NDFrame.mad(self, axis, skipna, level)

setattr(cls, "mad", mad)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/apply/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
pytest.param([1], {}, id="axis_from_args"),
pytest.param([], {"axis": 1}, id="axis_from_kwds"),
pytest.param([], {"numeric_only": True}, id="optional_kwds"),
pytest.param([1, None], {"numeric_only": True}, id="args_and_kwds"),
pytest.param([1, True], {"numeric_only": True}, id="args_and_kwds"),
],
)
@pytest.mark.parametrize("how", ["agg", "apply"])
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/frame/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,26 @@ def frame_of_index_cols():
}
)
return df


@pytest.fixture(
params=[
"any",
"all",
"count",
"sum",
"prod",
"max",
"min",
"mean",
"median",
"skew",
"kurt",
"sem",
"var",
"std",
"mad",
]
)
def reduction_functions(request):
return request.param
34 changes: 12 additions & 22 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,33 +1478,23 @@ def test_frame_any_with_timedelta(self):
expected = Series(data=[False, True])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize(
"func",
[
"any",
"all",
"count",
"sum",
"prod",
"max",
"min",
"mean",
"median",
"skew",
"kurt",
"sem",
"var",
"std",
"mad",
],
)
def test_reductions_deprecation_level_argument(self, frame_or_series, func):
def test_reductions_deprecation_level_argument(
self, frame_or_series, reduction_functions
):
# GH#39983
obj = frame_or_series(
[1, 2, 3], index=MultiIndex.from_arrays([[1, 2, 3], [4, 5, 6]])
)
with tm.assert_produces_warning(FutureWarning, match="level"):
getattr(obj, func)(level=0)
getattr(obj, reduction_functions)(level=0)

def test_reductions_skipna_none_raises(self, frame_or_series, reduction_functions):
if reduction_functions in ["count", "mad"]:
pytest.skip("Count does not accept skipna. Mad needs a depreaction cycle.")
obj = frame_or_series([1, 2, 3])
msg = 'For argument "skipna" expected type bool, received type NoneType.'
with pytest.raises(ValueError, match=msg):
getattr(obj, reduction_functions)(skipna=None)


class TestNuisanceColumns:
Expand Down