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 3 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 @@ -841,6 +841,7 @@ Groupby/resample/rolling
- Bug in :meth:`pandas.core.groupby.GroupBy.agg` where incorrect results are returned for uint64 columns. (:issue:`26310`)
- 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`)
- 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
13 changes: 13 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,19 @@ def test_closed(self):
with pytest.raises(ValueError):
df.rolling(window=3, closed='neither')

@pytest.mark.parametrize("closed", ["neither", "left"])
@pytest.mark.parametrize(
"func", ["std", "mean", "median", "sum", "max", "min", "var"])
def test_closed_empty(self, closed, func):
Copy link
Contributor

Choose a reason for hiding this comment

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

we have a fixture for these - look in pandas/conftest

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it is all_numeric_reductions but prod function fails since it is not implemented for Rolling. I will add kurt and skew to the list

Copy link
Contributor

Choose a reason for hiding this comment

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

you may need to define a separate fixture to avoid some issues

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

# GH 26005
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)()
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