[Numpy-discussion] Rec array: numpy.rec vs numpy.array with complex dtype
Fri Jun 26 14:59:57 CDT 2009
Pierre, thanks for your response. I have some follow up questions.
> a np.recarray is a subclass of ndarray with structured dtype, where
> fields can be accessed has attributes (as in 'yourarray.yourfield')
> instead of as items (as in yourarray['yourfield']).
Is this the only substantial thing added in the recarray class? The fact
that you can access some fields via attribute notation? We haven't been
using this feature anyhow ... (what happens with the field names have
Is the recarray class still being developed actively?
My favorite way to get a np.recarray is to define a standard ndarray
> w/ complex dtype, and then take a view as a recarray
> >>> np.array([(1,10),(2,20)],dtype=[('a',int),
Is the purpose of this basically to use the property of recarrays of
accessing fields as attributes? Or do you have other reasons why you like
> >>> x = np.array([(1,10),(2,20)],dtype=[('a',int),('b',int)])
> >>> rx = x.view(np.recarray)
> >>> type(x)
> <type 'numpy.void'>
> >>> type(rx)
> <type 'numpy.void'>
In : x = np.rec.fromrecords([(0,1,'a'),(2,0,'b')],names = ['A','B'])
In : x
Out: (0, 1, 'a')
In : type(x)
Out: <class 'numpy.core.records.record'>
In : np.version.version
> 3) We've heard talk about "complex data types" in numpy in general.
> Is there some good place we can read about this more extensively?
I think the proper term is 'structured data type', or 'structured
Do you recommend a place we can learn about the interesting things one can
do with structured data types? Or is the on-line documentation on the scipy
site the best as of now?
> You have to construct your dtype explicitly, as in "dtype=[('A',
> '<i4'), ('B', '<i4'), ('C', '|S1')]".
> np.rec.fromrecords processes the array and try to guess the best type
> for each field, but it's slow and not always correct
Evidently. But sometimes (in fact, a lot of times, in our particular
applications), the type inference works fine and the slowdown is not large
enough to be noticeable. And of course in the recarray constructors one
can override the type inference by including a 'dtype' or 'formats' argument
as well. Obviously we can write constructor functions that include type
inference algorithms of our own, ... but having a "standard" way to do this,
with best practices maintained in the numpy core would be quite useful
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Numpy-discussion