Dave Hirschfeld <dave.hirschfeld@...> writes: > > It appears that you can't assign an ndarray to a memoryview slice? Is > this correct/expected behaviour? > > Whilst there are a couple of possible workarounds it would be nice if > this just worked as it's a slightly surprising deviation from the > ndarray behaviour. > > Broken example: > ``` > In [3]: %%cython > ...: cimport cython > ...: import numpy as np > ...: cimport numpy as np > ...: > ...: cpdef np.ndarray[np.float64_t, ndim=2] f(): > ...: cdef np.ndarray[np.float64_t, ndim=2] x = np.ones([3, 4], > dtype=np.float64) > ...: cdef double[:,:] y = np.zeros([3, 4], dtype=np.float64) > ...: cdef np.ndarray[np.float64_t, ndim=1] row > ...: cdef int idx > ...: for idx, row in enumerate(x): > ...: y[idx] = row > ...: return np.asarray(y) > > In [4]: f() > Traceback (most recent call last): > > File "<ipython-input-4-0ec059b9bfe1>", line 1, in <module> > f() > > File "_cython_magic_5f2586693ddbf044815dae01d800bc0c.pyx", line 5, in > _cython_magic_5f2586693ddbf044815dae01d800bc0c.f > (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_5f2586693ddbf044815d > ae01d800bc0c.c:2086) > > File "_cython_magic_5f2586693ddbf044815dae01d800bc0c.pyx", line 11, in > _cython_magic_5f2586693ddbf044815dae01d800bc0c.f > (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_5f2586693ddbf044815d > ae01d800bc0c.c:1935) > ```
The error which I cutoff above is: Traceback (most recent call last): File "<ipython-input-4-0ec059b9bfe1>", line 1, in <module> f() File "_cython_magic_5f2586693ddbf044815dae01d800bc0c.pyx", line 5, in _cython_magic_5f2586693ddbf044815dae01d800bc0c.f (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_5f2586693ddbf044815d ae01d800bc0c.c:2086) File "_cython_magic_5f2586693ddbf044815dae01d800bc0c.pyx", line 11, in _cython_magic_5f2586693ddbf044815dae01d800bc0c.f (C:\Users\dhirschfeld\.ipython\cython\_cython_magic_5f2586693ddbf044815d ae01d800bc0c.c:1935) TypeError: only length-1 arrays can be converted to Python scalars _______________________________________________ cython-devel mailing list cython-devel@python.org https://mail.python.org/mailman/listinfo/cython-devel