Skip to content

Commit 02779e3

Browse files
committed
use isnan on problem function
1 parent cfc5c29 commit 02779e3

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

pandas/_libs/window.pyx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from cython cimport Py_ssize_t
66
from libcpp.deque cimport deque
77

88
from libc.stdlib cimport malloc, free
9+
from libc.math cimport isnan
910

1011
import numpy as np
1112
cimport numpy as cnp
@@ -649,25 +650,20 @@ cdef inline double calc_var(int64_t minp, int ddof, double nobs,
649650
return result
650651

651652

652-
cdef inline void add_var(double val, double *nobs, double *mean_x,
653+
cdef inline void add_var(const double val, double *nobs, double *mean_x,
653654
double *ssqdm_x) nogil:
654655
""" add a value from the var calc """
655656
cdef double delta
657+
# `isnan` instead of equality as fix for GH-21813
658+
if isnan(val):
659+
return
660+
656661
nobs[0] = nobs[0] + 1
657-
# Not NaN
658-
if val == val:
659-
# a part of Welford's method for the online variance-calculation
660-
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
661-
delta = val - mean_x[0]
662-
mean_x[0] = mean_x[0] + delta / nobs[0]
663-
ssqdm_x[0] = ssqdm_x[0] + ((nobs[0] - 1) * delta ** 2) / nobs[0]
664-
else:
665-
# XXX
666-
# `nobs[0] = nobs[0] + 1` should be in the if branch
667-
# but something goes wrong with MSVC 2017 causing the whole
668-
# path to optimize out, uncoditionally adding and
669-
# backing out as a hack to fix
670-
nobs[0] = nobs[0] - 1
662+
# a part of Welford's method for the online variance-calculation
663+
# https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
664+
delta = val - mean_x[0]
665+
mean_x[0] = mean_x[0] + delta / nobs[0]
666+
ssqdm_x[0] = ssqdm_x[0] + ((nobs[0] - 1) * delta ** 2) / nobs[0]
671667

672668

673669
cdef inline void remove_var(double val, double *nobs, double *mean_x,

0 commit comments

Comments
 (0)