[Numpy-discussion] ndarray from column data

Francesc Alted faltet@pytables....
Fri Jul 3 03:11:34 CDT 2009


A Thursday 02 July 2009 20:15:13 Dan Yamins escrigué:
> > What's wrong with recarrays?  In any case, if you need a true ndarray
> > object
> > you can always do:
> >
> > ndarr = recarr.view(np.ndarray)
> >
> > and you are done.
>
> I have a question about this though.  The object "ndarr" will consist of
> "records", e.g.:
>
>    In [96]: type(ndarr[0])
>    Out[96]: <class 'numpy.core.records.record'>
>
> If ndarr had been defined directly via ndarray constructors, then you'd
> get:
>
>    In [99]: type(ndarr[0])
>    Out[99]: <type 'numpy.void'>
>
> I guess this might not be such a big deal.   But what is the difference
> between the "record" class nad the "void" class?    Does this matter for
> anything at all?

The most apparent difference is that the record class implements accessors to 
fields as attributes:

In [17]: b = np.rec.array([(1,2.,'abc')], 'i4,f8,S3')

In [18]: b[0]
Out[18]: (1, 2.0, 'abc')

In [19]: b[0].f0
Out[19]: 1

In [20]: b[0].f2
Out[20]: 'abc'

while void type does not:

In [15]: a = np.array([(1,2.,'abc')], 'i4,f8,S3')

In [21]: a[0].f0
---------------------------------------------------------------------------
AttributeError: 'numpy.void' object has no attribute 'f0'

and you must make use of __getitem__() instead:

In [22]: a[0]['f0']
Out[22]: 1

In [23]: a[0]['f2']
Out[23]: 'abc'

Cheers,

-- 
Francesc Alted


More information about the Numpy-discussion mailing list