diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index e6bc422b52e89..8767a0c2d5ea1 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -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 ^^^^^^^^^ diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window.pyx index 48b554ca02a9d..3305fea06f003 100644 --- a/pandas/_libs/window.pyx +++ b/pandas/_libs/window.pyx @@ -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(win) if sl == NULL: raise MemoryError("skiplist_init failed") @@ -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(win) if skiplist == NULL: raise MemoryError("skiplist_init failed") diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index bc6946cbade4c..31baf4475214f 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -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