Skip to content

Commit 58cbf81

Browse files
ihsansecerjreback
authored andcommitted
BUG: Fix skiplist init error with empty window (#26940)
1 parent 06a3a03 commit 58cbf81

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ Groupby/Resample/Rolling
742742
- Bug in :meth:`pandas.core.groupby.SeriesGroupBy.transform` where transforming an empty group would raise a ``ValueError`` (:issue:`26208`)
743743
- 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`)
744744
- Bug in :meth:`pandas.core.groupby.GroupBy.agg` where incorrect results are returned for uint64 columns. (:issue:`26310`)
745+
- Bug in :meth:`pandas.core.window.Rolling.median` and :meth:`pandas.core.window.Rolling.quantile` where MemoryError is raised with empty window (:issue:`26005`)
745746

746747
Reshaping
747748
^^^^^^^^^

pandas/_libs/window.pyx

+9
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,10 @@ def roll_median_c(ndarray[float64_t] values, int64_t win, int64_t minp,
10991099
use_mock=False)
11001100
output = np.empty(N, dtype=float)
11011101

1102+
if win == 0:
1103+
output[:] = NaN
1104+
return output
1105+
11021106
sl = skiplist_init(<int>win)
11031107
if sl == NULL:
11041108
raise MemoryError("skiplist_init failed")
@@ -1486,6 +1490,11 @@ def roll_quantile(ndarray[float64_t, cast=True] values, int64_t win,
14861490
minp, index, closed,
14871491
use_mock=False)
14881492
output = np.empty(N, dtype=float)
1493+
1494+
if win == 0:
1495+
output[:] = NaN
1496+
return output
1497+
14891498
skiplist = skiplist_init(<int>win)
14901499
if skiplist == NULL:
14911500
raise MemoryError("skiplist_init failed")

pandas/tests/test_window.py

+11
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,17 @@ def tests_empty_df_rolling(self, roller):
608608
result = DataFrame(index=pd.DatetimeIndex([])).rolling(roller).sum()
609609
tm.assert_frame_equal(result, expected)
610610

611+
def test_empty_window_median_quantile(self):
612+
# GH 26005
613+
expected = pd.Series([np.nan, np.nan, np.nan])
614+
roll = pd.Series(np.arange(3)).rolling(0)
615+
616+
result = roll.median()
617+
tm.assert_series_equal(result, expected)
618+
619+
result = roll.quantile(0.1)
620+
tm.assert_series_equal(result, expected)
621+
611622
def test_missing_minp_zero(self):
612623
# https://github.com/pandas-dev/pandas/pull/18921
613624
# minp=0

0 commit comments

Comments
 (0)