Skip to content

Commit f9058fa

Browse files
mzeitlin11CGe0516
authored andcommitted
CLN: nancorr cleanups (pandas-dev#42757)
1 parent adb7664 commit f9058fa

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

pandas/_libs/algos.pyx

+10-11
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,10 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
325325
cdef:
326326
Py_ssize_t i, j, xi, yi, N, K
327327
bint minpv
328-
ndarray[float64_t, ndim=2] result
328+
float64_t[:, ::1] result
329329
ndarray[uint8_t, ndim=2] mask
330330
int64_t nobs = 0
331-
float64_t vx, vy, meanx, meany, divisor, prev_meany, prev_meanx, ssqdmx
332-
float64_t ssqdmy, covxy
331+
float64_t vx, vy, dx, dy, meanx, meany, divisor, ssqdmx, ssqdmy, covxy
333332

334333
N, K = (<object>mat).shape
335334

@@ -352,13 +351,13 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
352351
vx = mat[i, xi]
353352
vy = mat[i, yi]
354353
nobs += 1
355-
prev_meanx = meanx
356-
prev_meany = meany
357-
meanx = meanx + 1 / nobs * (vx - meanx)
358-
meany = meany + 1 / nobs * (vy - meany)
359-
ssqdmx = ssqdmx + (vx - meanx) * (vx - prev_meanx)
360-
ssqdmy = ssqdmy + (vy - meany) * (vy - prev_meany)
361-
covxy = covxy + (vx - meanx) * (vy - prev_meany)
354+
dx = vx - meanx
355+
dy = vy - meany
356+
meanx += 1 / nobs * dx
357+
meany += 1 / nobs * dy
358+
ssqdmx += (vx - meanx) * dx
359+
ssqdmy += (vy - meany) * dy
360+
covxy += (vx - meanx) * dy
362361

363362
if nobs < minpv:
364363
result[xi, yi] = result[yi, xi] = NaN
@@ -370,7 +369,7 @@ def nancorr(const float64_t[:, :] mat, bint cov=False, minp=None):
370369
else:
371370
result[xi, yi] = result[yi, xi] = NaN
372371

373-
return result
372+
return result.base
374373

375374
# ----------------------------------------------------------------------
376375
# Pairwise Spearman correlation

0 commit comments

Comments
 (0)