[Numpy-discussion] PyArray_SETITEM with object arrays in Cython

Dag Sverre Seljebotn dagss@student.matnat.uio...
Wed Mar 4 05:18:43 CST 2009


Wes McKinney wrote:
> This still doesn't explain why the buffer interface was slow.
I finally remembered to look at this; there seems to be a problem in 
your code:
> def reindexObject(ndarray[object, ndim=1] index,
>                   ndarray[object, ndim=1] arr,
>                   dict idxMap):
>     '''
>     Using the provided new index, a given array, and a mapping of 
> index-value
>     correpondences in the value array, return a new ndarray conforming to
>     the new index.
>     '''
>     cdef object idx, value
>
>     cdef int length  = index.shape[0]
>     cdef ndarray[object, ndim = 1] result = np.empty(length, dtype=object)
>   
>     cdef int i = 0
>     for i from 0 <= i < length:
>         idx = index[i]
>         if not PyDict_Contains(idxMap, idx):
>             result[i] = None
>             continue
>         value = arr[idxMap[idx]]
>         result[i] = value
>     return result

The problem is with arr[idxMap[idx]]. The result from idxMap[idx] is a 
Python object which leads to non-efficient indexing. Use 
arr[<int>idxMap[idx]] instead.

Dag Sverre


More information about the Numpy-discussion mailing list