[Numpy-discussion] retrieving type objects for void array-scalar objects
Francesc Altet
faltet at carabos.com
Sun Feb 5 04:33:04 EST 2006
El dv 03 de 02 del 2006 a les 22:05 +0100, en/na N. Volbers va escriure:
> Is there some way to retrieve the type object directly from the array (not using any existing row) using only the name of the item? I have checked the dtype attribute, but I could only get the character representation for the item types (e.g. 'f4').
Ops, I've just discovered a new way to get the type in a simpler way:
In [17]:dtype = numpy.dtype({'names': ['name', 'weight'],'formats':
['U30', 'f4']})
In [18]:a = numpy.array([(u'Bill', 71.2), (u'Fred', 94.3)], dtype=dtype)
In [20]:a.dtype.fields['name'][0].type
Out[20]:<type 'unicodescalar'>
In [21]:a.dtype.fields['weight'][0].type
Out[21]:<type 'float32scalar'>
For nested types, something like this should work:
ntype = a.dtype.fields['name'][0].fields['nested_field'][0].type
By the way, you will need numpy 0.9.5 (at least) for this to work.
Incidentally, Travis, what do you think about allowing:
In [30]:a.dtype.fields['weight']
Out[30]:dtype('<f4')
instead of current:
In [30]:a.dtype.fields['weight']
Out[30]:(dtype('<f4'), 120)
That way, users can access more easily to nested types:
ntype = a.dtype.fields['name'].fields['nested_field'].type
However, you will loose the info about the byteoffset/stride.
Alternatively, perhaps it would be interesting to give the dtype an
__getitem__ method so that we can do:
ntype = a.dtype['name']['nested_field'].type
In case of a dtype with no fields (i.e. a plain array), the
dtype[something] would raise a 'KeyError'. This approach would let the
current dtype.fields method untouched. I can work in a patch for this,
if you find it useful.
Cheers,
--
>0,0< Francesc Altet http://www.carabos.com/
V V Cárabos Coop. V. Enjoy Data
"-"
More information about the NumPy-Discussion
mailing list