Velocity Reviews > Numpy.array with dtype works on list of tuples not on list of lists?

# Numpy.array with dtype works on list of tuples not on list of lists?

Alex van der Spek
Guest
Posts: n/a

 09-18-2011
Why does this not work?

>>> dat=[[1,2,3],[4,5,6]]
>>> col=[('a','f4'),('b','f4'),('c','f4')]
>>> arr=numpy.array(dat,dtype=col)

Traceback (most recent call last):
File "<pyshell#91>", line 1, in <module>
arr=numpy.array(dat,dtype=col)
TypeError: expected a readable buffer object
>>>

But this does:

>>> dat=[(1,2,3),(4,5,6)]
>>> arr=numpy.array(dat,dtype=col)
>>> arr

array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4')])
>>>

The only difference that the object is a list of tuples now?

Thanks for clarification,
Alex van der Spek

Philip Semanchuk
Guest
Posts: n/a

 09-18-2011

On Sep 18, 2011, at 11:55 AM, Alex van der Spek wrote:

> Why does this not work?
>
>>>> dat=[[1,2,3],[4,5,6]]
>>>> col=[('a','f4'),('b','f4'),('c','f4')]
>>>> arr=numpy.array(dat,dtype=col)

>
> Traceback (most recent call last):
> File "<pyshell#91>", line 1, in <module>
> arr=numpy.array(dat,dtype=col)
> TypeError: expected a readable buffer object
>
> But this does:
>
>>>> dat=[(1,2,3),(4,5,6)]
>>>> arr=numpy.array(dat,dtype=col)
>>>> arr

> array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', '<f4'), ('b', '<f4'), ('c', '<f4')])
>
> The only difference that the object is a list of tuples now?

I don't know why you're seeing what you're seeing, but if you don't get answer here you could try asking on the numpy list.

Good luck
Philip

Robert Kern
Guest
Posts: n/a

 09-18-2011
On 9/18/11 10:55 AM, Alex van der Spek wrote:
> Why does this not work?
>
>>>> dat=[[1,2,3],[4,5,6]]
>>>> col=[('a','f4'),('b','f4'),('c','f4')]
>>>> arr=numpy.array(dat,dtype=col)

>
> Traceback (most recent call last):
> File "<pyshell#91>", line 1, in <module>
> arr=numpy.array(dat,dtype=col)
> TypeError: expected a readable buffer object
>>>>

>
> But this does:
>
>>>> dat=[(1,2,3),(4,5,6)]
>>>> arr=numpy.array(dat,dtype=col)
>>>> arr

> array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)], dtype=[('a', '<f4'), ('b', '<f4'),
> ('c', '<f4')])
>>>>

>
> The only difference that the object is a list of tuples now?

numpy questions are best asked on the numpy mailing list:

http://www.scipy.org/Mailing_Lists

To answer your question, though, numpy.array() needs to figure out a lot of
different things about the input data simultaneously, in particular its shape.
Structured arrays (i.e. with elements that have individual fields as above) pose
a new problem in that its individual elements are sequences themselves. In order
to help it decide whether it should recurse down into a sequence to find its
elements or decide that the sequence *is* an element in its own right, we
settled on the convention that tuples are to be considered elements and that
lists are sequences of elements.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
an underlying truth."
-- Umberto Eco