|
From: Francesc A. <fa...@ca...> - 2006-02-05 12:33:00
|
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 (no=
t 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 =3D numpy.dtype({'names': ['name', 'weight'],'formats':
['U30', 'f4']})
In [18]:a =3D numpy.array([(u'Bill', 71.2), (u'Fred', 94.3)], dtype=3Ddtype=
)
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 =3D 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 =3D 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 =3D 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,
--=20
>0,0< Francesc Altet http://www.carabos.com/
V V C=E1rabos Coop. V. Enjoy Data
"-"
|