From 447413cdc1f39422aab49676d79e603cf26b78c7 Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Tue, 12 Apr 2022 23:03:41 -0700 Subject: [PATCH 1/7] Change range to prevent segfault See https://github.com/pandas-dev/pandas/issues/46760 --- pandas/_libs/window/aggregations.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/_libs/window/aggregations.pyx b/pandas/_libs/window/aggregations.pyx index 8191084da924f..e58f4c2aa8e42 100644 --- a/pandas/_libs/window/aggregations.pyx +++ b/pandas/_libs/window/aggregations.pyx @@ -1592,7 +1592,7 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights, with nogil: - for i in range(win_n): + for i in range(min(win_n, n)): add_weighted_var(values[i], weights[i], &t, &sum_w, &mean, &nobs) From ed92d31b0edbacd72fa3ee5cb063d385775c5ed6 Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Wed, 13 Apr 2022 15:44:07 -0700 Subject: [PATCH 2/7] Update v1.5.0.rst with doc of fix for issue 46760 --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 358d9447b131d..fe94e3035d9dc 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -599,7 +599,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`) - Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`) - Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`) -- +- Bug in :meth:`.Rolling.var` would segfault when calculated weighted variance with large window size (:issue:`46760`) Reshaping ^^^^^^^^^ From d3dc82b94a73d1959ff768301f06a5924b5ba20f Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Wed, 13 Apr 2022 15:45:25 -0700 Subject: [PATCH 3/7] typo --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index fe94e3035d9dc..560eb78dbff00 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -599,7 +599,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`) - Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`) - Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`) -- Bug in :meth:`.Rolling.var` would segfault when calculated weighted variance with large window size (:issue:`46760`) +- Bug in :meth:`.Rolling.var` would segfault when calculating weighted variance with large window size (:issue:`46760`) Reshaping ^^^^^^^^^ From 951bbcf3c953260c762ead5b8ae1039524661f99 Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Wed, 13 Apr 2022 20:01:41 -0700 Subject: [PATCH 4/7] Improve language for fix of weighted variance segfault --- doc/source/whatsnew/v1.5.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 560eb78dbff00..0e6eb5d0a1fbb 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -599,7 +599,7 @@ Groupby/resample/rolling - Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`) - Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`) - Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`) -- Bug in :meth:`.Rolling.var` would segfault when calculating weighted variance with large window size (:issue:`46760`) +- Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`) Reshaping ^^^^^^^^^ From 6bfa895d2322c80adc872a0606572aa03015feae Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Wed, 13 Apr 2022 20:32:10 -0700 Subject: [PATCH 5/7] Added test for issue #46772 --- pandas/tests/window/test_win_type.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index c356c9bdc7742..f1b837816ee73 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -676,3 +676,13 @@ def test_cmov_window_special_linear_range(win_types_special, step): .mean(**kwds[win_types_special]) ) tm.assert_series_equal(xp, rs) + + +@td.skip_if_no_scipy +def test_weighted_var_big_window_no_segfault(): + # Github Issue #46772 + x = Series(0) + result = x.rolling(window=16, center=True, win_type='triang').var() + expected = Series(np.NaN) + + tm.assert_series_equal(result, expected) From a932053ec27c908f559053c6f6b8207a3ffddf51 Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Wed, 13 Apr 2022 20:44:19 -0700 Subject: [PATCH 6/7] style - change single quotes to double --- pandas/tests/window/test_win_type.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index f1b837816ee73..3e93b89d710d1 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -682,7 +682,7 @@ def test_cmov_window_special_linear_range(win_types_special, step): def test_weighted_var_big_window_no_segfault(): # Github Issue #46772 x = Series(0) - result = x.rolling(window=16, center=True, win_type='triang').var() + result = x.rolling(window=16, center=True, win_type="triang").var() expected = Series(np.NaN) tm.assert_series_equal(result, expected) From 6ed2f4c735ae8f60e18807b93a665ee2b1d84606 Mon Sep 17 00:00:00 2001 From: Jose Ortiz Date: Thu, 14 Apr 2022 07:59:25 -0700 Subject: [PATCH 7/7] Add fixtures `win_types` and `center` to test signature --- pandas/tests/window/test_win_type.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/window/test_win_type.py b/pandas/tests/window/test_win_type.py index 3e93b89d710d1..11e288591fe75 100644 --- a/pandas/tests/window/test_win_type.py +++ b/pandas/tests/window/test_win_type.py @@ -679,10 +679,10 @@ def test_cmov_window_special_linear_range(win_types_special, step): @td.skip_if_no_scipy -def test_weighted_var_big_window_no_segfault(): +def test_weighted_var_big_window_no_segfault(win_types, center): # Github Issue #46772 x = Series(0) - result = x.rolling(window=16, center=True, win_type="triang").var() + result = x.rolling(window=16, center=center, win_type=win_types).var() expected = Series(np.NaN) tm.assert_series_equal(result, expected)