Fwd: Named dtype array: Difference between a[0]['name'] and a['name'][0]?
dear all, can anybody tell me, why nobody is answering this question? is this the wrong place to ask? or does nobody know an answer? björn
This is the right place to ask, it's just that it can take time to get an answer because people who might know the answer may not have the time to respond immediately. The short answer is that this is not really a "normal" bug, but it could be considered a "design" bug (although the issues may not be straightforward to resolve). What that means is that it may not be changed in the short term  and you should just use the first spelling. Structured arrays can be a confusing area of NumPy for several of reasons. You've constructed an example that touches on several of them. You have a datatype that is a "structure" array with one member ("tuple"). That member contains a 2vector of integers. First of all, it is important to remember that with Python, doing a['tuple'][0] = (1,2) is equivalent to b = a['tuple']; b[0] = (1,2). In like manner, a[0]['tuple'] = (1,2) is equivalent to b = a[0]; b['tuple'] = (1,2). To understand the behavior, we need to dissect both code paths and what happens. You built a (3,) array of those elements in 'a'. When you write b = a['tuple'] you should probably be getting a (3,) array of (2,)integers, but as there is currently no formal dtype support for (n,)integers as a general dtype in NumPy, you get back a (3,2) array of integers which is the closest thing that NumPy can give you. Setting the [0] row of this object via a['tuple'][0] = (1,2) works just fine and does what you would expect. On the other hand, when you type: b = a[0] you are getting back an arrayscalar which is a particularly interesting kind of array scalar that can hold records. This new object is formally of type numpy.void and it holds a "scalar representation" of anything that fits under the "VOID" basic dtype. For some reason: b['tuple'] = [1,2] is not working. On my system I'm getting a different error: TypeError: object of type 'int' has no len() I think this should be filed as a bug on the issue tracker which is for the time being here: http://projects.scipy.org/numpy The problem is ultimately the void>copyswap function being called in voidtype_setfields if someone wants to investigate. I think this behavior should work. Travis On May 21, 2012, at 1:50 PM, bmu wrote:
dear all,
can anybody tell me, why nobody is answering this question? is this the wrong place to ask? or does nobody know an answer?
björn From: bmu
Subject: Named dtype array: Difference between a[0]['name'] and a['name'][0]? Date: May 20, 2012 6:45:03 AM CDT To: numpydiscussion@scipy.org I came acroos a question on stackoverflow (http://stackoverflow.com/q/9470604) and I am wondering if this is a bug
import numpy as np dt = np.dtype([('tuple', (int, 2))]) a = np.zeros(3, dt) type(a['tuple'][0]) # ndarray type(a[0]['tuple']) # ndarray
a['tuple'][0] = (1,2) # ok a[0]['tuple'] = (1,2) # ValueError: shapemismatch on array construction Could somebody explain this behaviour (either in this mailing list or on stackoverflow)?
bmu
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
On Mon, May 21, 2012 at 4:37 PM, Travis Oliphant
This is the right place to ask, it's just that it can take time to get an answer because people who might know the answer may not have the time to respond immediately.
The short answer is that this is not really a "normal" bug, but it could be considered a "design" bug (although the issues may not be straightforward to resolve). What that means is that it may not be changed in the short term  and you should just use the first spelling.
Structured arrays can be a confusing area of NumPy for several of reasons. You've constructed an example that touches on several of them. You have a datatype that is a "structure" array with one member ("tuple"). That member contains a 2vector of integers.
First of all, it is important to remember that with Python, doing a['tuple'][0] = (1,2) is equivalent to b = a['tuple']; b[0] = (1,2). In like manner, a[0]['tuple'] = (1,2) is equivalent to b = a[0]; b['tuple'] = (1,2).
To understand the behavior, we need to dissect both code paths and what happens. You built a (3,) array of those elements in 'a'. When you write b = a['tuple'] you should probably be getting a (3,) array of (2,)integers, but as there is currently no formal dtype support for (n,)integers as a general dtype in NumPy, you get back a (3,2) array of integers which is the closest thing that NumPy can give you. Setting the [0] row of this object via
a['tuple'][0] = (1,2)
works just fine and does what you would expect.
On the other hand, when you type:
b = a[0]
you are getting back an arrayscalar which is a particularly interesting kind of array scalar that can hold records. This new object is formally of type numpy.void and it holds a "scalar representation" of anything that fits under the "VOID" basic dtype.
For some reason:
b['tuple'] = [1,2]
is not working. On my system I'm getting a different error: TypeError: object of type 'int' has no len()
I think this should be filed as a bug on the issue tracker which is for the time being here: http://projects.scipy.org/numpy
The problem is ultimately the void>copyswap function being called in voidtype_setfields if someone wants to investigate. I think this behavior should work.
Just playing around I found this to be odd, though I guess makes some
sense given your comments
[~/]
[12]: b['tuple'] = [(1,2)]
[~/]
[13]: b
[13]: ([1, 0],)
[~/]
[14]: a
[14]:
array([([1, 0],), ([0, 0],), ([0, 0],)],
dtype=[('tuple', '
thanks a lot. I updated the question on stackoverflow and opened a ticket
http://projects.scipy.org/numpy/ticket/2139
björn
Am Montag, 21. Mai 2012, 15:37:36 schrieb Travis Oliphant:
This is the right place to ask, it's just that it can take time to get an
answer because people who might know the answer may not have the time to
respond immediately.
The short answer is that this is not really a "normal" bug, but it could be
considered a "design" bug (although the issues may not be straightforward to
resolve). What that means is that it may not be changed in the short term
 and you should just use the first spelling.
Structured arrays can be a confusing area of NumPy for several of reasons.
You've constructed an example that touches on several of them. You have a
datatype that is a "structure" array with one member ("tuple"). That member
contains a 2vector of integers.
First of all, it is important to remember that with Python, doing a['tuple']
[0] = (1,2) is equivalent to b = a['tuple']; b[0] = (1,2). In like manner,
a[0]['tuple'] = (1,2) is equivalent to b = a[0]; b['tuple'] = (1,2).
To understand the behavior, we need to dissect both code paths and what
happens. You built a (3,) array of those elements in 'a'. When you write b
= a['tuple'] you should probably be getting a (3,) array of (2,)integers, but
as there is currently no formal dtype support for (n,)integers as a general
dtype in NumPy, you get back a (3,2) array of integers which is the closest
thing that NumPy can give you. Setting the [0] row of this object via
a['tuple'][0] = (1,2)
works just fine and does what you would expect.
On the other hand, when you type:
b = a[0]
you are getting back an arrayscalar which is a particularly interesting kind
of array scalar that can hold records. This new object is formally of type
numpy.void and it holds a "scalar representation" of anything that fits under
the "VOID" basic dtype.
For some reason:
b['tuple'] = [1,2]
is not working. On my system I'm getting a different error: TypeError: object
of type 'int' has no len()
I think this should be filed as a bug on the issue tracker which is for the
time being here: http://projects.scipy.org/numpy
The problem is ultimately the void>copyswap function being called in
voidtype_setfields if someone wants to investigate. I think this behavior
should work.
Travis
On May 21, 2012, at 1:50 PM, bmu wrote:
dear all,
can anybody tell me, why nobody is answering this question? is this the wrong
place to ask? or does nobody know an answer?
björn
From: bmu
participants (4)

bmu

diehose

Skipper Seabold

Travis Oliphant