Skip to content

Commit 03fb204

Browse files
committed
ENH: fast zip function for ndarrays
1 parent 73ab1c1 commit 03fb204

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

pandas/src/tseries.pyx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ cimport cython
44
from numpy cimport *
55

66
from cpython cimport (PyDict_New, PyDict_GetItem, PyDict_SetItem,
7-
PyDict_Contains, PyDict_Keys)
7+
PyDict_Contains, PyDict_Keys,
8+
Py_INCREF, PyTuple_SET_ITEM,
9+
PyTuple_SetItem,
10+
PyTuple_New)
811
from cpython cimport PyFloat_Check
912

1013
import numpy as np
@@ -394,9 +397,51 @@ def fast_unique_multiple_list(list lists):
394397

395398
return uniques
396399

400+
def fast_zip(list ndarrays):
401+
'''
402+
For zipping multiple ndarrays into an ndarray of tuples
403+
'''
404+
cdef:
405+
Py_ssize_t i, j, k, n
406+
ndarray[object] result
407+
flatiter it
408+
object val, tup
409+
410+
k = len(ndarrays)
411+
n = len(ndarrays[0])
412+
413+
result = np.empty(n, dtype=object)
414+
415+
# initialize tuples on first pass
416+
arr = ndarrays[0]
417+
it = <flatiter> PyArray_IterNew(arr)
418+
for i in range(n):
419+
val = PyArray_GETITEM(arr, PyArray_ITER_DATA(it))
420+
tup = PyTuple_New(k)
421+
422+
PyTuple_SET_ITEM(tup, 0, val)
423+
Py_INCREF(val)
424+
result[i] = tup
425+
PyArray_ITER_NEXT(it)
426+
427+
for j in range(1, k):
428+
arr = ndarrays[j]
429+
it = <flatiter> PyArray_IterNew(arr)
430+
if len(arr) != n:
431+
raise ValueError('all arrays but be same length')
432+
433+
for i in range(n):
434+
val = PyArray_GETITEM(arr, PyArray_ITER_DATA(it))
435+
PyTuple_SET_ITEM(result[i], j, val)
436+
Py_INCREF(val)
437+
PyArray_ITER_NEXT(it)
438+
439+
return result
440+
397441
include "skiplist.pyx"
398442
include "groupby.pyx"
399443
include "moments.pyx"
400444
include "reindex.pyx"
401445
include "generated.pyx"
402446
include "parsing.pyx"
447+

0 commit comments

Comments
 (0)