Skip to content

Commit 9f8e3cf

Browse files
author
Daniel Saxton
committed
Rank outside loop
1 parent 5d9fd7e commit 9f8e3cf

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

pandas/_libs/algos.pyx

+8-4
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1):
296296
cdef:
297297
Py_ssize_t i, j, xi, yi, N, K
298298
ndarray[float64_t, ndim=2] result
299+
ndarray[float64_t, ndim=2] ranked_mat
299300
ndarray[float64_t, ndim=1] maskedx
300301
ndarray[float64_t, ndim=1] maskedy
301302
ndarray[uint8_t, ndim=2] mask
@@ -307,6 +308,11 @@ def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1):
307308
result = np.empty((K, K), dtype=np.float64)
308309
mask = np.isfinite(mat).view(np.uint8)
309310

311+
ranked_mat = np.empty((N, K), dtype=np.float64)
312+
313+
for i in range(K):
314+
ranked_mat[:, i] = rank_1d_float64(mat[:, i])
315+
310316
for xi in range(K):
311317
for yi in range(xi + 1):
312318
nobs = 0
@@ -322,11 +328,9 @@ def nancorr_spearman(const float64_t[:, :] mat, Py_ssize_t minp=1):
322328
j = 0
323329
for i in range(N):
324330
if mask[i, xi] and mask[i, yi]:
325-
maskedx[j] = mat[i, xi]
326-
maskedy[j] = mat[i, yi]
331+
maskedx[j] = ranked_mat[i, xi]
332+
maskedy[j] = ranked_mat[i, yi]
327333
j += 1
328-
maskedx = rank_1d_float64(maskedx)
329-
maskedy = rank_1d_float64(maskedy)
330334

331335
mean = (nobs + 1) / 2.
332336

0 commit comments

Comments
 (0)