Skip to content

BUG: Fix empty closed window issue with rolling min and max #27140

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 6 commits into from
Jul 1, 2019
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/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,7 @@ Groupby/resample/rolling
- Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where MemoryError is raised with empty window (:issue:`26005`)
- Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where incorrect results are returned with ``closed='left'`` and ``closed='neither'`` (:issue:`26005`)
- Improved :class:`pandas.core.window.Rolling`, :class:`pandas.core.window.Window` and :class:`pandas.core.window.EWM` functions to exclude nuisance columns from results instead of raising errors and raise a ``DataError`` only if all columns are nuisance (:issue:`12537`)
- Bug in :meth:`pandas.core.window.Rolling.max` and :meth:`pandas.core.window.Rolling.min` where incorrect results are returned with an empty variable window`` (:issue:`26005`)

Reshaping
^^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,11 @@ cdef _roll_min_max_variable(ndarray[numeric] values,

# if right is open then the first window is empty
close_offset = 0 if endi[0] > starti[0] else 1
# first window's size
curr_win_size = endi[0] - starti[0]

for i in range(endi[0], endi[N-1]):
if not Q.empty():
if not Q.empty() and curr_win_size > 0:
output[i-1+close_offset] = calc_mm(
minp, nobs, values[Q.front()])
else:
Expand Down Expand Up @@ -1344,7 +1346,7 @@ cdef _roll_min_max_variable(ndarray[numeric] values,
Q.push_back(i)
W.push_back(i)

if not Q.empty():
if not Q.empty() and curr_win_size > 0:
output[N-1] = calc_mm(minp, nobs, values[Q.front()])
else:
output[N-1] = NaN
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ def win_types_special(request):
return request.param


@pytest.fixture(params=["sum", "mean", "median", "max", "min",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok this is fine; can you do a followup to use this fixture as much as possible in this file?

"var", "std", "kurt", "skew"])
def arithmetic_win_operators(request):
return request.param


class Base:

_nan_locs = np.arange(20, 40)
Expand Down Expand Up @@ -522,6 +528,18 @@ def test_closed(self):
with pytest.raises(ValueError):
df.rolling(window=3, closed='neither')

@pytest.mark.parametrize("closed", ["neither", "left"])
def test_closed_empty(self, closed, arithmetic_win_operators):
# GH 26005
func_name = arithmetic_win_operators
ser = pd.Series(data=np.arange(5),
index=pd.date_range("2000", periods=5, freq="2D"))
roll = ser.rolling("1D", closed=closed)

result = getattr(roll, func_name)()
expected = pd.Series([np.nan] * 5, index=ser.index)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("func", ['min', 'max'])
def test_closed_one_entry(self, func):
# GH24718
Expand Down