@@ -6,6 +6,7 @@ from cython cimport Py_ssize_t
6
6
from libcpp.deque cimport deque
7
7
8
8
from libc.stdlib cimport malloc, free
9
+ from libc.math cimport isnan
9
10
10
11
import numpy as np
11
12
cimport numpy as cnp
@@ -649,25 +650,20 @@ cdef inline double calc_var(int64_t minp, int ddof, double nobs,
649
650
return result
650
651
651
652
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,
653
654
double * ssqdm_x) nogil:
654
655
""" add a value from the var calc """
655
656
cdef double delta
657
+ # `isnan` instead of equality as fix for GH-21813
658
+ if isnan(val):
659
+ return
660
+
656
661
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 ]
671
667
672
668
673
669
cdef inline void remove_var(double val, double * nobs, double * mean_x,
0 commit comments