Skip to content

BUG: Fix skiplist init error with empty window #26940

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
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 @@ -742,6 +742,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` and :meth:`pandas.core.window.Rolling.quantile` where MemoryError is raised with empty window (:issue:`26005`)

Reshaping
^^^^^^^^^
Expand Down
9 changes: 9 additions & 0 deletions pandas/_libs/window.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,10 @@ def roll_median_c(ndarray[float64_t] values, int64_t win, int64_t minp,
use_mock=False)
output = np.empty(N, dtype=float)

if win == 0:
output[:] = NaN
return output

sl = skiplist_init(<int>win)
if sl == NULL:
raise MemoryError("skiplist_init failed")
Expand Down Expand Up @@ -1486,6 +1490,11 @@ def roll_quantile(ndarray[float64_t, cast=True] values, int64_t win,
minp, index, closed,
use_mock=False)
output = np.empty(N, dtype=float)

if win == 0:
output[:] = NaN
return output

skiplist = skiplist_init(<int>win)
if skiplist == NULL:
raise MemoryError("skiplist_init failed")
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/test_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,17 @@ def tests_empty_df_rolling(self, roller):
result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
tm.assert_frame_equal(result, expected)

def test_empty_window_median_quantile(self):
# GH 26005
expected = pd.Series([np.nan, np.nan, np.nan])
roll = pd.Series(np.arange(3)).rolling(0)

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

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

def test_missing_minp_zero(self):
# https://github.com/pandas-dev/pandas/pull/18921
# minp=0
Expand Down