Skip to content

Commit 7cf3e2b

Browse files
jh-wuPingviinituutti
authored andcommitted
BUG: Offset-based rolling window, with only one raw in dataframe and closed='left', max and min functions make python crash (pandas-dev#24811)
1 parent 7d915e7 commit 7cf3e2b

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,7 @@ Plotting
17591759
Groupby/Resample/Rolling
17601760
^^^^^^^^^^^^^^^^^^^^^^^^
17611761

1762+
- Bug in :func:`pandas.core.window.Rolling.min` and :func:`pandas.core.window.Rolling.max` with ``closed='left'``, a datetime-like index and only one entry in the series leading to segfault (:issue:`24718`)
17621763
- Bug in :func:`pandas.core.groupby.GroupBy.first` and :func:`pandas.core.groupby.GroupBy.last` with ``as_index=False`` leading to the loss of timezone information (:issue:`15884`)
17631764
- Bug in :meth:`DateFrame.resample` when downsampling across a DST boundary (:issue:`8531`)
17641765
- Bug in date anchoring for :meth:`DateFrame.resample` with offset :class:`Day` when n > 1 (:issue:`24127`)

pandas/_libs/window.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,10 @@ cdef _roll_min_max_variable(ndarray[numeric] values,
13391339
Q.push_back(i)
13401340
W.push_back(i)
13411341

1342-
output[N-1] = calc_mm(minp, nobs, values[Q.front()])
1342+
if not Q.empty():
1343+
output[N-1] = calc_mm(minp, nobs, values[Q.front()])
1344+
else:
1345+
output[N-1] = NaN
13431346

13441347
return output
13451348

pandas/tests/test_window.py

+20
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,26 @@ def test_closed(self):
520520
with pytest.raises(ValueError):
521521
df.rolling(window=3, closed='neither')
522522

523+
@pytest.mark.parametrize("func", ['min', 'max'])
524+
def test_closed_one_entry(self, func):
525+
# GH24718
526+
ser = pd.Series(data=[2], index=pd.date_range('2000', periods=1))
527+
result = getattr(ser.rolling('10D', closed='left'), func)()
528+
tm.assert_series_equal(result, pd.Series([np.nan], index=ser.index))
529+
530+
@pytest.mark.parametrize("func", ['min', 'max'])
531+
def test_closed_one_entry_groupby(self, func):
532+
# GH24718
533+
ser = pd.DataFrame(data={'A': [1, 1, 2], 'B': [3, 2, 1]},
534+
index=pd.date_range('2000', periods=3))
535+
result = getattr(
536+
ser.groupby('A', sort=False)['B'].rolling('10D', closed='left'),
537+
func)()
538+
exp_idx = pd.MultiIndex.from_arrays(arrays=[[1, 1, 2], ser.index],
539+
names=('A', None))
540+
expected = pd.Series(data=[np.nan, 3, np.nan], index=exp_idx, name='B')
541+
tm.assert_series_equal(result, expected)
542+
523543
@pytest.mark.parametrize("input_dtype", ['int', 'float'])
524544
@pytest.mark.parametrize("func,closed,expected", [
525545
('min', 'right', [0.0, 0, 0, 1, 2, 3, 4, 5, 6, 7]),

0 commit comments

Comments
 (0)