Skip to content

DEPR: Enforce rolling.count min_periods deprecation #48839

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 7 commits into from
Oct 19, 2022
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/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Removal of prior version deprecations/changes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)

.. ---------------------------------------------------------------------------
.. _whatsnew_200.performance:
Expand Down
29 changes: 7 additions & 22 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,21 +1936,21 @@ def aggregate(self, func, *args, **kwargs):
"""
>>> s = pd.Series([2, 3, np.nan, 10])
>>> s.rolling(2).count()
0 1.0
0 NaN
1 2.0
2 1.0
3 1.0
dtype: float64
>>> s.rolling(3).count()
0 1.0
1 2.0
0 NaN
1 NaN
2 2.0
3 2.0
dtype: float64
>>> s.rolling(4).count()
0 1.0
1 2.0
2 2.0
0 NaN
1 NaN
2 NaN
3 3.0
dtype: float64
"""
Expand All @@ -1960,22 +1960,7 @@ def aggregate(self, func, *args, **kwargs):
agg_method="count",
)
def count(self, numeric_only: bool = False):
if self.min_periods is None:
warnings.warn(
(
"min_periods=None will default to the size of window "
"consistent with other methods in a future version. "
"Specify min_periods=0 instead."
),
FutureWarning,
stacklevel=find_stack_level(),
)
self.min_periods = 0
result = super().count()
self.min_periods = None
else:
result = super().count(numeric_only)
return result
return super().count(numeric_only)

@doc(
template_header,
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/window/test_base_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ def get_window_bounds(self, num_values, min_periods, center, closed, step):
),
],
)
@pytest.mark.filterwarnings("ignore:min_periods:FutureWarning")
def test_rolling_forward_window(
frame_or_series, func, np_func, expected, np_kwargs, step
):
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/window/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ def test_getitem_multiple(self, roll_frame):
"mean",
"min",
"max",
pytest.param(
"count",
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
),
"count",
"kurt",
"skew",
],
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series):
expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0]

# GH 31302
with tm.assert_produces_warning(FutureWarning):
result = frame_or_series(values).rolling(3).count()
result = frame_or_series(values).rolling(3, min_periods=0).count()
expected = frame_or_series(expected_counts)
tm.assert_equal(result, expected)

Expand Down
10 changes: 4 additions & 6 deletions pandas/tests/window/test_rolling_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
[
[np.mean, "mean", {}],
[np.nansum, "sum", {}],
pytest.param(
[
lambda x: np.isfinite(x).astype(float).sum(),
"count",
{},
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
),
],
[np.median, "median", {}],
[np.min, "min", {}],
[np.max, "max", {}],
Expand All @@ -50,12 +49,11 @@ def test_series(series, compare_func, roll_func, kwargs, step):
[
[np.mean, "mean", {}],
[np.nansum, "sum", {}],
pytest.param(
[
lambda x: np.isfinite(x).astype(float).sum(),
"count",
{},
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
),
],
[np.median, "median", {}],
[np.min, "min", {}],
[np.max, "max", {}],
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/window/test_timeseries_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,7 @@ def test_freqs_ops(self, freq, op, result_data):
[
"sum",
"mean",
pytest.param(
"count",
marks=pytest.mark.filterwarnings("ignore:min_periods:FutureWarning"),
),
"count",
"median",
"std",
"var",
Expand Down