[Numpy-discussion] Initializing array from buffer

Nathaniel Smith njs at pobox.com
Mon Nov 17 15:52:16 EST 2014

On Sun, Nov 16, 2014 at 5:42 PM, Andrea Arteaga <andyspiros at gmail.com> wrote:
> Hello.
> Using the numpy.frombuffer function [1] one can initialize a numpy array
> using an existing python object that implements the buffer protocol [2].
> This is great, but currently this function supports only 1D buffers, even if
> the provided buffer is multidimensional and it exposes all information about
> its structure (shape, strides, data type).

np.frombuffer is not often used, and I'm not sure if it's been updated
for the new py3 buffer protocol. (The old buffer protocol only
supported 1d buffers.)

Have you tried just using np.asarray? It seems to work fine with
multidimensional memoryview objects at least, which use the new buffer

In [12]: a = np.ones((2, 3))

In [13]: a_buf = memoryview(a)

In [14]: a_buf
Out[14]: <memory at 0x7ffd071a2d60>

In [15]: np.asarray(a_buf)
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]])

Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh

More information about the NumPy-Discussion mailing list