
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 np.array([(1), (3), (5)], dtype=np.dtype(int)) array([1, 3, 5]) np.array((1,2, 3,4, 5,6)) array([1, 2, 3, 4, 5, 6])
Thanks Bruce

On Fri, May 13, 2011 at 10:58 AM, Bruce Southey <bsouthey@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 >>> np.array([(1), (3), (5)], dtype=np.dtype(int)) array([1, 3, 5]) >>> np.array((1,2, 3,4, 5,6)) array([1, 2, 3, 4, 5, 6])
Using a view works, (and direct assignment of dtype)
a=np.array([1,2, 3,4, 5,6]).view(([('foo', int)])) a array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) b = a.copy() b array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)])
a1 = np.array([1,2, 3,4, 5,6]) a1.dtype
Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) TypeError: expected a readable buffer object dtype('int32')
a1.dtype = np.dtype([('foo', int)]) a1 array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
Josef
Thanks Bruce _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Hi, just a comment since I first thought the solution below might not be what Bruce was looking for, but having realised it's probably what he's been asking for... On 13 May 2011, at 17:20, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 10:58 AM, Bruce Southey <bsouthey@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. ...
Using a view works, (and direct assignment of dtype)
a=np.array([1,2, 3,4, 5,6]).view(([('foo', int)])) a array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) b = a.copy() b array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)])
Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) TypeError: expected a readable buffer object
a1 = np.array([1,2, 3,4, 5,6]) a1.dtype dtype('int32') a1.dtype = np.dtype([('foo', int)]) a1 array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
This is a 1-d structured array, yet a is in fact not the same as a['foo']:
a['foo'] array([ 1, 2, 3, 4, 5, 6]) a['foo'].shape (6,) a.shape (6,) a['foo']+np.arange(6) array([ 1, 3, 5, 7, 9, 11]) a+np.arange(6) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray' a[0]+1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'numpy.void' and 'int'
Thus I am wondering why broadcasting should not be possible in this case, and if it really isn't, the first error message certainly is not very helpful... (maybe inevitably though, since type() of a structured array is the same as for a "plain" array (unlike for a record array). Cheers, Derek

On Fri, May 13, 2011 at 3:11 PM, Derek Homeier <derek@astro.physik.uni-goettingen.de> wrote:
Hi,
just a comment since I first thought the solution below might not be what Bruce was looking for, but having realised it's probably what he's been asking for...
On 13 May 2011, at 17:20, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 10:58 AM, Bruce Southey <bsouthey@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. ...
Using a view works, (and direct assignment of dtype)
a=np.array([1,2, 3,4, 5,6]).view(([('foo', int)])) a array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) b = a.copy() b array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)])
Traceback (most recent call last): File "<pyshell#36>", line 1, in <module> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) TypeError: expected a readable buffer object
a1 = np.array([1,2, 3,4, 5,6]) a1.dtype dtype('int32') a1.dtype = np.dtype([('foo', int)]) a1 array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
This is a 1-d structured array, yet a is in fact not the same as a['foo']:
>>> a['foo'] array([ 1, 2, 3, 4, 5, 6]) >>> a['foo'].shape (6,) >>> a.shape (6,) >>> a['foo']+np.arange(6) array([ 1, 3, 5, 7, 9, 11]) >>> a+np.arange(6) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'
funny things with subclasses, I saw this also in an error message in another package with a subclass of ndarray, should read: 'this ndarray-(sub)class' and 'that ndarray-(sub)class' for some messages it's just getting used to the "coding" "TypeError: expected a readable buffer object" translates into "I cannot interpret the data as the desired array" e.g. list of list or list of tuples
>>> a[0]+1 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'numpy.void' and 'int'
Thus I am wondering why broadcasting should not be possible in this case,
Even a 1 column table is still a table (or a list of records), and a 1 item row is still a row. Josef
and if it really isn't, the first error message certainly is not very helpful... (maybe inevitably though, since type() of a structured array is the same as for a "plain" array (unlike for a record array).
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On 13 May 2011, at 22:04, josef.pktd@gmail.com wrote:
Thus I am wondering why broadcasting should not be possible in this case,
Even a 1 column table is still a table (or a list of records), and a 1 item row is still a row.
True, but even multiplying the shape (6, ) array e.g. with a shape (2, 6) array works just fine. I assume it does not work for a structured array because the records _could_ in principle have incompatible data types (like str), even though in this case they haven't... Cheers, Derek

On 05/13/2011 03:04 PM, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 3:11 PM, Derek Homeier <derek@astro.physik.uni-goettingen.de> wrote:
Hi,
just a comment since I first thought the solution below might not be what Bruce was looking for, but having realised it's probably what he's been asking for...
On 13 May 2011, at 17:20, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 10:58 AM, Bruce Southey<bsouthey@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. ... Using a view works, (and direct assignment of dtype)
a=np.array([1,2, 3,4, 5,6]).view(([('foo', int)])) a array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) b = a.copy() b array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) Traceback (most recent call last): File "<pyshell#36>", line 1, in<module> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) TypeError: expected a readable buffer object
a1 = np.array([1,2, 3,4, 5,6]) a1.dtype dtype('int32') a1.dtype = np.dtype([('foo', int)]) a1 array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) This is a 1-d structured array, yet a is in fact not the same as a['foo']:
a['foo'] array([ 1, 2, 3, 4, 5, 6]) a['foo'].shape (6,) a.shape (6,) a['foo']+np.arange(6) array([ 1, 3, 5, 7, 9, 11]) a+np.arange(6) Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray' funny things with subclasses, I saw this also in an error message in another package with a subclass of ndarray, should read: 'this ndarray-(sub)class' and 'that ndarray-(sub)class'
for some messages it's just getting used to the "coding"
"TypeError: expected a readable buffer object" translates into "I cannot interpret the data as the desired array" e.g. list of list or list of tuples
a[0]+1 Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: unsupported operand type(s) for +: 'numpy.void' and 'int'
Thus I am wondering why broadcasting should not be possible in this case, Even a 1 column table is still a table (or a list of records), and a 1 item row is still a row.
Josef
and if it really isn't, the first error message certainly is not very helpful... (maybe inevitably though, since type() of a structured array is the same as for a "plain" array (unlike for a record array).
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Thanks for the replies, especially the dtype example Josef!
So this should be reported as a bug? Bruce

On Fri, May 13, 2011 at 5:03 PM, Bruce Southey <bsouthey@gmail.com> wrote:
On 05/13/2011 03:04 PM, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 3:11 PM, Derek Homeier <derek@astro.physik.uni-goettingen.de> wrote:
Hi,
just a comment since I first thought the solution below might not be what Bruce was looking for, but having realised it's probably what he's been asking for...
On 13 May 2011, at 17:20, josef.pktd@gmail.com wrote:
On Fri, May 13, 2011 at 10:58 AM, Bruce Southey<bsouthey@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. ... Using a view works, (and direct assignment of dtype)
> a=np.array([1,2, 3,4, 5,6]).view(([('foo', int)])) > a array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) > b = a.copy() > b array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')])
> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) Traceback (most recent call last): File "<pyshell#36>", line 1, in<module> a1 = np.array([1,2, 3,4, 5,6]).astype([('foo', int)]) TypeError: expected a readable buffer object
> a1 = np.array([1,2, 3,4, 5,6]) > a1.dtype dtype('int32') > a1.dtype = np.dtype([('foo', int)]) > a1 array([(1,), (2,), (3,), (4,), (5,), (6,)], dtype=[('foo', '<i4')]) This is a 1-d structured array, yet a is in fact not the same as a['foo']:
>>> a['foo'] array([ 1, 2, 3, 4, 5, 6]) >>> a['foo'].shape (6,) >>> a.shape (6,) >>> a['foo']+np.arange(6) array([ 1, 3, 5, 7, 9, 11]) >>> a+np.arange(6) Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray' funny things with subclasses, I saw this also in an error message in another package with a subclass of ndarray, should read: 'this ndarray-(sub)class' and 'that ndarray-(sub)class'
for some messages it's just getting used to the "coding"
"TypeError: expected a readable buffer object" translates into "I cannot interpret the data as the desired array" e.g. list of list or list of tuples
>>> a[0]+1 Traceback (most recent call last): File "<stdin>", line 1, in<module> TypeError: unsupported operand type(s) for +: 'numpy.void' and 'int'
Thus I am wondering why broadcasting should not be possible in this case, Even a 1 column table is still a table (or a list of records), and a 1 item row is still a row.
Josef
and if it really isn't, the first error message certainly is not very helpful... (maybe inevitably though, since type() of a structured array is the same as for a "plain" array (unlike for a record array).
Cheers, Derek
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion Thanks for the replies, especially the dtype example Josef!
So this should be reported as a bug?
It's by design, a structured array takes a list of tuples and is an array of records, -- and tuples and records are not lists or arrays and don't behave that way. I have no opinion about whether it should be considered a bug, I just learned to live with it. Josef
Bruce
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Fri, May 13, 2011 at 09:58, Bruce Southey <bsouthey@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

On Fri, May 13, 2011 at 4:38 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Fri, May 13, 2011 at 09:58, Bruce Southey <bsouthey@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@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

On Sun, May 15, 2011 at 20:49, Bruce Southey <bsouthey@gmail.com> wrote:
On Fri, May 13, 2011 at 4:38 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Fri, May 13, 2011 at 09:58, Bruce Southey <bsouthey@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')])
I am still 'digesting' this and what it means. In any case, many thanks!
You make record arrays from lists of tuples (or lists of lists ... of tuples). If you want one-field records, you need one-element tuples. -- 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
participants (4)
-
Bruce Southey
-
Derek Homeier
-
josef.pktd@gmail.com
-
Robert Kern