Skip to content

[BUG]: Rolling selected too large windows with PeriodIndex #36730

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 9 commits into from
Oct 3, 2020
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrameGroupBy.apply` raising error with ``np.nan`` group(s) when ``dropna=False`` (:issue:`35889`)
- Bug in :meth:`Rolling.sum()` returned wrong values when dtypes where mixed between float and integer and axis was equal to one (:issue:`20649`, :issue:`35596`)
- Bug in :meth:`Rolling.count` returned ``np.nan`` with :class:`pandas.api.indexers.FixedForwardWindowIndexer` as window, ``min_periods=0`` and only missing values in window (:issue:`35579`)
- Bug where :class:`pandas.core.window.Rolling` produces incorrect window sizes when using a ``PeriodIndex`` (:issue:`34225`)

Reshaping
^^^^^^^^^
Expand Down
13 changes: 11 additions & 2 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,6 @@ def validate(self):
):

self._validate_monotonic()
freq = self._validate_freq()

# we don't allow center
if self.center:
Expand All @@ -1943,7 +1942,7 @@ def validate(self):

# this will raise ValueError on non-fixed freqs
self.win_freq = self.window
self.window = freq.nanos
self.window = self._determine_window_length()
self.win_type = "freq"

# min_periods must be an integer
Expand All @@ -1963,6 +1962,16 @@ def validate(self):
"closed only implemented for datetimelike and offset based windows"
)

def _determine_window_length(self) -> Union[int, float]:
"""
Calculate freq for PeriodIndexes based on Index freq. Can not use
nanos, because asi8 of PeriodIndex is not in nanos
"""
freq = self._validate_freq()
if isinstance(self._on, ABCPeriodIndex):
return freq.nanos / (self._on.freq.nanos / self._on.freq.n)
return freq.nanos

def _validate_monotonic(self):
"""
Validate monotonic (increasing or decreasing).
Expand Down
31 changes: 31 additions & 0 deletions pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,34 @@ def test_rolling_on_df_transposed():

result = df.T.rolling(min_periods=1, window=2).sum().T
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
("index", "window"),
[
(
pd.period_range(start="2020-01-01 08:00", end="2020-01-01 08:08", freq="T"),
"2T",
),
(
pd.period_range(
start="2020-01-01 08:00", end="2020-01-01 12:00", freq="30T"
),
"1h",
),
],
)
@pytest.mark.parametrize(
("func", "values"),
[
("min", [np.nan, 0, 0, 1, 2, 3, 4, 5, 6]),
("max", [np.nan, 0, 1, 2, 3, 4, 5, 6, 7]),
("sum", [np.nan, 0, 1, 3, 5, 7, 9, 11, 13]),
],
)
def test_rolling_period_index(index, window, func, values):
# GH: 34225
ds = pd.Series([0, 1, 2, 3, 4, 5, 6, 7, 8], index=index)
result = getattr(ds.rolling(window, closed="left"), func)()
expected = pd.Series(values, index=index)
tm.assert_series_equal(result, expected)