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 4 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 @@ -709,6 +709,7 @@ Groupby/Resample/Rolling
- Bug in :meth:`pandas.core.groupby.SeriesGroupBy.transform` where transforming an empty group would raise a ``ValueError`` (:issue:`26208`)
- 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` where incorrect results are returned with ``closed='left'`` and ``closed='neither'`` (:issue:`26005`)

Reshaping
^^^^^^^^^
Expand Down
27 changes: 14 additions & 13 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1112,21 +1112,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 @@ -1137,6 +1131,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
14 changes: 14 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,20 @@ 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(self, closed, expected):
# GH 26005
ser = pd.Series(data=np.arange(10),
index=pd.date_range('2000', periods=10))
result = ser.rolling('3D', closed=closed).median()
expected = pd.Series(expected, index=ser.index)
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