From 417ca64cce66dc50bb952f3940c732ce44a39b44 Mon Sep 17 00:00:00 2001 From: Junhua Wu Date: Thu, 17 Jan 2019 11:02:21 +1100 Subject: [PATCH 1/3] BUG: GH24718 fixed --- pandas/_libs/window.pyx | 5 ++++- pandas/tests/test_window.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/window.pyx b/pandas/_libs/window.pyx index 5f2c0233a0f13..e8f3de64c3823 100644 --- a/pandas/_libs/window.pyx +++ b/pandas/_libs/window.pyx @@ -1339,7 +1339,10 @@ cdef _roll_min_max_variable(ndarray[numeric] values, Q.push_back(i) W.push_back(i) - output[N-1] = calc_mm(minp, nobs, values[Q.front()]) + if not Q.empty(): + output[N-1] = calc_mm(minp, nobs, values[Q.front()]) + else: + output[N-1] = NaN return output diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 412f70a3cb516..6e77e6515f022 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -520,6 +520,26 @@ def test_closed(self): with pytest.raises(ValueError): df.rolling(window=3, closed='neither') + @pytest.mark.parametrize("func", [('min'), ('max')]) + def test_closed_one_entry(self, func): + # GH24718 + ser = pd.Series(data=[2], index=pd.date_range('2000', periods=1)) + result = getattr(ser.rolling('10D', closed='left'), func)() + tm.assert_series_equal(result, pd.Series([np.nan], index=ser.index)) + + @pytest.mark.parametrize("func", [('min'), ('max')]) + def test_closed_one_entry_groupby(self, func): + # GH24718 + ser = pd.DataFrame(data={'A': [1, 1, 2], 'B': [3, 2, 1]}, + index=pd.date_range('2000', periods=3)) + result = getattr( + ser.groupby('A', sort=False)['B'].rolling('10D', closed='left'), + func)() + exp_idx = pd.MultiIndex.from_arrays(arrays=[[1, 1, 2], ser.index], + names=('A', None)) + expected = pd.Series(data=[np.nan, 3, np.nan], index=exp_idx, name='B') + tm.assert_series_equal(result, expected) + @pytest.mark.parametrize("input_dtype", ['int', 'float']) @pytest.mark.parametrize("func,closed,expected", [ ('min', 'right', [0.0, 0, 0, 1, 2, 3, 4, 5, 6, 7]), From e3947480c4fa2726da189e7a424d931afb98e5ac Mon Sep 17 00:00:00 2001 From: Junhua Wu Date: Thu, 17 Jan 2019 14:43:44 +1100 Subject: [PATCH 2/3] BUG: closes GH24718 --- doc/source/whatsnew/v0.24.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 5213120b33f06..bcd1abba30a67 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1808,6 +1808,7 @@ Plotting Groupby/Resample/Rolling ^^^^^^^^^^^^^^^^^^^^^^^^ +- 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`) - 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`) - Bug in :meth:`DateFrame.resample` when downsampling across a DST boundary (:issue:`8531`) - Bug in date anchoring for :meth:`DateFrame.resample` with offset :class:`Day` when n > 1 (:issue:`24127`) From 2e513db4688f9edf882c6cf5e065a6fd07161f6f Mon Sep 17 00:00:00 2001 From: Junhua Wu Date: Fri, 18 Jan 2019 09:31:08 +1100 Subject: [PATCH 3/3] BUG:GH24718 remove the unnecessary parens in pytest.mark.parametrize --- pandas/tests/test_window.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/test_window.py b/pandas/tests/test_window.py index 6e77e6515f022..e816d4c04344a 100644 --- a/pandas/tests/test_window.py +++ b/pandas/tests/test_window.py @@ -520,14 +520,14 @@ def test_closed(self): with pytest.raises(ValueError): df.rolling(window=3, closed='neither') - @pytest.mark.parametrize("func", [('min'), ('max')]) + @pytest.mark.parametrize("func", ['min', 'max']) def test_closed_one_entry(self, func): # GH24718 ser = pd.Series(data=[2], index=pd.date_range('2000', periods=1)) result = getattr(ser.rolling('10D', closed='left'), func)() tm.assert_series_equal(result, pd.Series([np.nan], index=ser.index)) - @pytest.mark.parametrize("func", [('min'), ('max')]) + @pytest.mark.parametrize("func", ['min', 'max']) def test_closed_one_entry_groupby(self, func): # GH24718 ser = pd.DataFrame(data={'A': [1, 1, 2], 'B': [3, 2, 1]},