[Numpy-discussion] Creating a 1-d structured array

Bruce Southey bsouthey at gmail.com
Sun May 15 21:49:15 EDT 2011


On Fri, May 13, 2011 at 4:38 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Fri, May 13, 2011 at 09:58, Bruce Southey <bsouthey at gmail.com> wrote:
>> Hi,
>> How do you create a 'single' structured array using np.array()?
>> Basically I am attempting to do something like this that does not work:
>> a=np.array([1,2, 3,4, 5,6], dtype=np.dtype([('foo', int)]))
>>
>> I realize that this is essentially redundant as if A is an 1-d array
>> then a structured array with a named field 'foo' is the same thing - A
>> would be A['foo'], just shorter.
>>
>> So if that is valid then a clearer error message is required to indicate
>> this and provide the suitable error message to address ticket 1264
>> (http://projects.scipy.org/numpy/ticket/1264).
>>
>> $ python
>> Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
>> [GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>  >>> import numpy as np
>>  >>> a=np.array([1,2, 3,4, 5,6], dtype=np.dtype([('foo', int)]))
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> TypeError: expected a readable buffer object
>>  >>> np.array([(1,2), (3,4), (5,6)], dtype=np.dtype([('foo', int),
>> ('foo2', int)]))
>> array([(1, 2), (3, 4), (5, 6)],
>>       dtype=[('foo', '<i8'), ('foo2', '<i8')])
>>  >>> a=np.array([(1), (3), (5)], dtype=np.dtype([('foo', int)]))
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> TypeError: expected a readable buffer object
>
> You are missing the commas:
>
> [~]
> |12> a=np.array([(1,), (3,), (5,)], dtype=np.dtype([('foo', int)]))
>
> [~]
> |13> a
> array([(1,), (3,), (5,)],
>      dtype=[('foo', '<i4')])
>
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless
> enigma that is made terrible by our own mad attempt to interpret it as
> though it had an underlying truth."
>   -- Umberto Eco
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
I am still 'digesting' this and what it means.
In any case, many thanks!
Bruce



More information about the NumPy-Discussion mailing list