Skip to content

BUG: Fix rolling median and quantile with closed='left' and closed='neither' (#26005) #26910

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
Jun 21, 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 @@ -744,6 +744,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.frame.DataFrame.groupby` where passing a :class:`pandas.core.groupby.grouper.Grouper` would return incorrect groups when using the ``.groups`` accessor (:issue:`26326`)
- 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`)

Reshaping
^^^^^^^^^
Expand Down
50 changes: 26 additions & 24 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1116,21 +1116,15 @@ def roll_median_c(ndarray[float64_t] values, int64_t win, int64_t minp,
if i == 0:

# setup
val = values[i]
if notnan(val):
nobs += 1
err = skiplist_insert(sl, val) != 1
if err:
break

else:

# calculate deletes
for j in range(start[i - 1], s):
for j in range(s, e):
val = values[j]
if notnan(val):
skiplist_remove(sl, val)
nobs -= 1
nobs += 1
err = skiplist_insert(sl, val) != 1
if err:
break

else:

# calculate adds
for j in range(end[i - 1], e):
Expand All @@ -1141,6 +1135,13 @@ def roll_median_c(ndarray[float64_t] values, int64_t win, int64_t minp,
if err:
break

# calculate deletes
for j in range(start[i - 1], s):
val = values[j]
if notnan(val):
skiplist_remove(sl, val)
nobs -= 1

if nobs >= minp:
midpoint = <int>(nobs / 2)
if nobs % 2:
Expand Down Expand Up @@ -1507,19 +1508,13 @@ def roll_quantile(ndarray[float64_t, cast=True] values, int64_t win,
if i == 0:

# setup
val = values[i]
if notnan(val):
nobs += 1
skiplist_insert(skiplist, val)

else:

# calculate deletes
for j in range(start[i - 1], s):
for j in range(s, e):
val = values[j]
if notnan(val):
skiplist_remove(skiplist, val)
nobs -= 1
nobs += 1
skiplist_insert(skiplist, val)

else:

# calculate adds
for j in range(end[i - 1], e):
Expand All @@ -1528,6 +1523,13 @@ def roll_quantile(ndarray[float64_t, cast=True] values, int64_t win,
nobs += 1
skiplist_insert(skiplist, val)

# calculate deletes
for j in range(start[i - 1], s):
val = values[j]
if notnan(val):
skiplist_remove(skiplist, val)
nobs -= 1

if nobs >= minp:
if nobs == 1:
# Single value in skip list
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,25 @@ def test_closed_min_max_minp(self, func, closed, expected):
expected = pd.Series(expected, index=ser.index)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("closed,expected", [
('right', [0, 0.5, 1, 2, 3, 4, 5, 6, 7, 8]),
('both', [0, 0.5, 1, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5]),
('neither', [np.nan, 0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5]),
('left', [np.nan, 0, 0.5, 1, 2, 3, 4, 5, 6, 7])
])
def test_closed_median_quantile(self, closed, expected):
# GH 26005
ser = pd.Series(data=np.arange(10),
index=pd.date_range('2000', periods=10))
roll = ser.rolling('3D', closed=closed)
expected = pd.Series(expected, index=ser.index)

result = roll.median()
tm.assert_series_equal(result, expected)

result = roll.quantile(0.5)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize('roller', ['1s', 1])
def tests_empty_df_rolling(self, roller):
# GH 15819 Verifies that datetime and integer rolling windows can be
Expand Down