Skip to content

Commit d558bce

Browse files
authored
REGR: Rolling.count setting min_periods after call (pandas-dev#39604)
1 parent c5a1348 commit d558bce

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

doc/source/whatsnew/v1.2.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`~DataFrame.to_pickle` failing to create bz2/xz compressed pickle files with ``protocol=5`` (:issue:`39002`)
2222
- Fixed regression in :func:`pandas.testing.assert_series_equal` and :func:`pandas.testing.assert_frame_equal` always raising ``AssertionError`` when comparing extension dtypes (:issue:`39410`)
2323
- Fixed regression in :meth:`~DataFrame.to_csv` opening ``codecs.StreamWriter`` in binary mode instead of in text mode and ignoring user-provided ``mode`` (:issue:`39247`)
24+
- Fixed regression in :meth:`core.window.rolling.Rolling.count` where the ``min_periods`` argument would be set to ``0`` after the operation (:issue:`39554`)
2425
-
2526

2627
.. ---------------------------------------------------------------------------

pandas/core/window/rolling.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,11 @@ def count(self):
15071507
FutureWarning,
15081508
)
15091509
self.min_periods = 0
1510-
return super().count()
1510+
result = super().count()
1511+
self.min_periods = None
1512+
else:
1513+
result = super().count()
1514+
return result
15111515

15121516
@doc(
15131517
template_header,

pandas/tests/window/test_api.py

+14
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,17 @@ def test_is_datetimelike_deprecated():
325325
s = Series(range(1)).rolling(1)
326326
with tm.assert_produces_warning(FutureWarning):
327327
assert not s.is_datetimelike
328+
329+
330+
@pytest.mark.filterwarnings("ignore:min_periods:FutureWarning")
331+
def test_dont_modify_attributes_after_methods(
332+
arithmetic_win_operators, closed, center, min_periods
333+
):
334+
# GH 39554
335+
roll_obj = Series(range(1)).rolling(
336+
1, center=center, closed=closed, min_periods=min_periods
337+
)
338+
expected = {attr: getattr(roll_obj, attr) for attr in roll_obj._attributes}
339+
getattr(roll_obj, arithmetic_win_operators)()
340+
result = {attr: getattr(roll_obj, attr) for attr in roll_obj._attributes}
341+
assert result == expected

pandas/tests/window/test_base_indexer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,10 @@ def test_rolling_forward_window(constructor, func, np_func, expected, np_kwargs)
159159

160160
# Check that the function output matches applying an alternative function
161161
# if min_periods isn't specified
162-
rolling3 = constructor(values).rolling(window=indexer)
162+
# GH 39604: After count-min_periods deprecation, apply(lambda x: len(x))
163+
# is equivalent to count after setting min_periods=0
164+
min_periods = 0 if func == "count" else None
165+
rolling3 = constructor(values).rolling(window=indexer, min_periods=min_periods)
163166
result3 = getattr(rolling3, func)()
164167
expected3 = constructor(rolling3.apply(lambda x: np_func(x, **np_kwargs)))
165168
tm.assert_equal(result3, expected3)

0 commit comments

Comments
 (0)