Skip to content

Commit 5b76b77

Browse files
authored
BUG: Prevent segfault in roll_weighted_var (#46772)
* Change range to prevent segfault See #46760 * Update v1.5.0.rst with doc of fix for issue 46760 * typo * Improve language for fix of weighted variance segfault * Added test for issue #46772 * style - change single quotes to double * Add fixtures `win_types` and `center` to test signature
1 parent 4ffdab3 commit 5b76b77

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ Groupby/resample/rolling
601601
- Bug in :meth:`GroupBy.cummax` with ``int64`` dtype with leading value being the smallest possible int64 (:issue:`46382`)
602602
- Bug in :meth:`GroupBy.max` with empty groups and ``uint64`` dtype incorrectly raising ``RuntimeError`` (:issue:`46408`)
603603
- Bug in :meth:`.GroupBy.apply` would fail when ``func`` was a string and args or kwargs were supplied (:issue:`46479`)
604-
-
604+
- Bug in :meth:`.Rolling.var` would segfault calculating weighted variance when window size was larger than data size (:issue:`46760`)
605605

606606
Reshaping
607607
^^^^^^^^^

pandas/_libs/window/aggregations.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1592,7 +1592,7 @@ def roll_weighted_var(const float64_t[:] values, const float64_t[:] weights,
15921592

15931593
with nogil:
15941594

1595-
for i in range(win_n):
1595+
for i in range(min(win_n, n)):
15961596
add_weighted_var(values[i], weights[i], &t,
15971597
&sum_w, &mean, &nobs)
15981598

pandas/tests/window/test_win_type.py

+10
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,13 @@ def test_cmov_window_special_linear_range(win_types_special, step):
676676
.mean(**kwds[win_types_special])
677677
)
678678
tm.assert_series_equal(xp, rs)
679+
680+
681+
@td.skip_if_no_scipy
682+
def test_weighted_var_big_window_no_segfault(win_types, center):
683+
# Github Issue #46772
684+
x = Series(0)
685+
result = x.rolling(window=16, center=center, win_type=win_types).var()
686+
expected = Series(np.NaN)
687+
688+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)