Skip to content

Commit dbb5b45

Browse files
adamkleinwesm
authored andcommitted
ENH: fix nancorr further
1 parent f9dd1ae commit dbb5b45

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

pandas/src/moments.pyx

+20-17
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def ewma(ndarray[double_t] input, double_t com):
252252

253253
@cython.boundscheck(False)
254254
@cython.wraparound(False)
255-
def nancorr(ndarray[float64_t, ndim=2] mat, na_ok=False):
255+
def nancorr(ndarray[float64_t, ndim=2] mat):
256256
cdef:
257257
Py_ssize_t i, j, xi, yi, N, K
258258
ndarray[float64_t, ndim=2] result
@@ -276,27 +276,30 @@ def nancorr(ndarray[float64_t, ndim=2] mat, na_ok=False):
276276
sumx += vx
277277
sumy += vy
278278

279-
meanx = sumx / nobs
280-
meany = sumy / nobs
279+
if nobs == 0:
280+
result[xi, yi] = result[yi, xi] = np.NaN
281+
else:
282+
meanx = sumx / nobs
283+
meany = sumy / nobs
281284

282-
# now the cov numerator
283-
sumx = 0
285+
# now the cov numerator
286+
sumx = 0
284287

285-
for i in range(N):
286-
if mask[i, xi] and mask[i, yi]:
287-
vx = mat[i, xi] - meanx
288-
vy = mat[i, yi] - meany
288+
for i in range(N):
289+
if mask[i, xi] and mask[i, yi]:
290+
vx = mat[i, xi] - meanx
291+
vy = mat[i, yi] - meany
289292

290-
sumx += vx * vy
291-
sumxx += vx * vx
292-
sumyy += vy * vy
293+
sumx += vx * vy
294+
sumxx += vx * vx
295+
sumyy += vy * vy
293296

294-
divisor = sqrt(sumxx * sumyy)
297+
divisor = sqrt(sumxx * sumyy)
295298

296-
if na_ok == 0:
297-
result[xi, yi] = result[yi, xi] = sumx / divisor if divisor != 0 else np.NaN
298-
else:
299-
result[xi, yi] = result[yi, xi] = sumx / divisor
299+
if divisor != 0:
300+
result[xi, yi] = result[yi, xi] = sumx / divisor
301+
else:
302+
result[xi, yi] = result[yi, xi] = np.NaN
300303

301304
return result
302305

0 commit comments

Comments
 (0)