From 47e567ba7137ede8d8ec4c2616025fdecc6c736f Mon Sep 17 00:00:00 2001 From: phofl Date: Thu, 1 Oct 2020 23:02:22 +0200 Subject: [PATCH] Fix bug with empty windows with count and FixedForwardWindowIndexer --- doc/source/whatsnew/v1.1.3.rst | 1 + pandas/core/window/rolling.py | 2 +- pandas/tests/window/test_base_indexer.py | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.3.rst b/doc/source/whatsnew/v1.1.3.rst index 15777abcb8084..8edfd5885fc28 100644 --- a/doc/source/whatsnew/v1.1.3.rst +++ b/doc/source/whatsnew/v1.1.3.rst @@ -58,6 +58,7 @@ Bug fixes - Bug in :meth:`Series.astype` showing too much precision when casting from ``np.float32`` to string dtype (:issue:`36451`) - Bug in :meth:`Series.isin` and :meth:`DataFrame.isin` when using ``NaN`` and a row length above 1,000,000 (:issue:`22205`) - Bug in :func:`cut` raising a ``ValueError`` when passed a :class:`Series` of labels with ``ordered=False`` (:issue:`36603`) +- Bug in :meth:`Rolling.count` returned ``np.nan`` with ``FixedForwardWindowIndexer`` as window, ``min_periods=0`` and only missing values in window (:issue:`35579`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/window/rolling.py b/pandas/core/window/rolling.py index 6ab42dda865e7..83a07a2abb484 100644 --- a/pandas/core/window/rolling.py +++ b/pandas/core/window/rolling.py @@ -2056,7 +2056,7 @@ def count(self): # when using a BaseIndexer subclass as a window if self.is_freq_type or isinstance(self.window, BaseIndexer): window_func = self._get_roll_func("roll_count") - return self._apply(window_func, center=self.center, name="count") + return self._apply(window_func, center=self.center, name="count", floor=0) return super().count() diff --git a/pandas/tests/window/test_base_indexer.py b/pandas/tests/window/test_base_indexer.py index f681b19d57600..2a47f7397b33f 100644 --- a/pandas/tests/window/test_base_indexer.py +++ b/pandas/tests/window/test_base_indexer.py @@ -253,3 +253,12 @@ def test_non_fixed_variable_window_indexer(closed, expected_data): result = df.rolling(indexer, closed=closed).sum() expected = DataFrame(expected_data, index=index) tm.assert_frame_equal(result, expected) + + +def test_fixed_forward_indexer_count(): + # GH: 35579 + df = DataFrame({"b": [None, None, None, 7]}) + indexer = FixedForwardWindowIndexer(window_size=2) + result = df.rolling(window=indexer, min_periods=0).count() + expected = DataFrame({"b": [0.0, 0.0, 1.0, 1.0]}) + tm.assert_frame_equal(result, expected)