Skip to content

Commit bfe10f0

Browse files
committed
TST: added more tests (GH26996)
1 parent b0f5baa commit bfe10f0

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,7 @@ Groupby/resample/rolling
11311131
- Bug in :meth:`DataFrame.groupby` when using nunique on axis=1 (:issue:`30253`)
11321132
- Bug in :meth:`GroupBy.quantile` with multiple list-like q value and integer column names (:issue:`30289`)
11331133
- Bug in :meth:`GroupBy.pct_change` and :meth:`core.groupby.SeriesGroupBy.pct_change` causes ``TypeError`` when ``fill_method`` is ``None`` (:issue:`30463`)
1134+
- Bug in :meth:`Rolling.count` and :meth:`Expanding.count` argument ``min_periods`` ignored (:issue:`26996`)
11341135

11351136
Reshaping
11361137
^^^^^^^^^

doc/source/whatsnew/v1.1.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ Plotting
133133
Groupby/resample/rolling
134134
^^^^^^^^^^^^^^^^^^^^^^^^
135135

136-
- Bug in :meth:`Rolling.count` and :meth:`Expanding.count` argument ``min_periods`` ignored (:issue:`26996`)
136+
-
137+
-
137138

138139
Reshaping
139140
^^^^^^^^^

pandas/tests/window/test_rolling.py

+32-4
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,36 @@ def test_min_periods1():
448448
tm.assert_series_equal(result, expected)
449449

450450

451-
def test_rolling_count_with_min_periods():
451+
@pytest.mark.parametrize("test_series", [True, False])
452+
def test_rolling_count_with_min_periods(test_series):
452453
# GH 26996
453-
result = Series(range(5)).rolling(3, min_periods=3).count()
454-
expected = Series([np.nan, np.nan, 3.0, 3.0, 3.0])
455-
tm.assert_series_equal(result, expected)
454+
if test_series:
455+
result = Series(range(5)).rolling(3, min_periods=3).count()
456+
expected = Series([np.nan, np.nan, 3.0, 3.0, 3.0])
457+
tm.assert_series_equal(result, expected)
458+
else:
459+
result = DataFrame(range(5)).rolling(3, min_periods=3).count()
460+
expected = DataFrame([np.nan, np.nan, 3.0, 3.0, 3.0])
461+
tm.assert_frame_equal(result, expected)
462+
463+
464+
@pytest.mark.parametrize("test_series", [True, False])
465+
def test_rolling_count_default_min_periods_with_null_values(test_series):
466+
# GH 26996
467+
# We need rolling count to have default min_periods=0,
468+
# as the method is meant to count how many non-null values,
469+
# we want to by default produce a valid count even if
470+
# there are very few valid entries in the window
471+
values = [1, 2, 3, np.nan, 4, 5, 6]
472+
expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0]
473+
474+
if test_series:
475+
ser = Series(values)
476+
result = ser.rolling(3).count()
477+
expected = Series(expected_counts)
478+
tm.assert_series_equal(result, expected)
479+
else:
480+
df = DataFrame(values)
481+
result = df.rolling(3).count()
482+
expected = DataFrame(expected_counts)
483+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)