[Numpy-discussion] PyTables, array and recarray

Francesc Alted falted at pytables.org
Fri May 30 14:33:25 EDT 2008


A Friday 30 May 2008, Hanno Klemm escrigué:
> Hi,
>
> I try to save the contents of a numpy recarray with PyTables into a
> file. That works well, however, if I then try to retrieve the data, I
> get back an array with matching dtypes rather than a recarray.
>
> What is the best way to get a recarray back, or if that's not
> possible what's the most efficient way to convert an array to a
> recarray?
>
> This is what I am doing at the moment:
>
> Python 2.5 (r25:51908, Oct  4 2006, 17:28:51)
> [GCC 3.2.3 20030502 (Red Hat Linux 3.2.3-52)] on linux2
> Type "help", "copyright", "credits" or "license" for more
> information.
>
> >>> import numpy as N
> >>> import tables as t
> >>> num = 2
> >>> a = N.recarray(num, formats='i4,f8,f8',names='id,x,y')
> >>> a['id'] = [3,4]
> >>> a['x'] = [3.4,4.5]
> >>> a['y'] = [4.6,4.5]
> >>> a
>
> recarray([(3, 3.3999999999999999, 4.5999999999999996), (4, 4.5,
> 4.5)], dtype=[('id', '<i4'), ('x', '<f8'), ('y', '<f8')])
>
> >>> f = t.openFile('test.h5', 'w')
> >>> f.createTable('/', 'test', a)
>
> /test (Table(2L,)) ''
>   description := {
>   "id": Int32Col(shape=(), dflt=0, pos=0),
>   "x": Float64Col(shape=(), dflt=0.0, pos=1),
>   "y": Float64Col(shape=(), dflt=0.0, pos=2)}
>   byteorder := 'little'
>   chunkshape := (409,)
>
> >>> f.close()
> >>> f = t.openFile('test.h5', 'r')
> >>> b = f.root.test[:]
> >>> b
>
> array([(3, 3.3999999999999999, 4.5999999999999996), (4, 4.5, 4.5)],
>       dtype=[('id', '<i4'), ('x', '<f8'), ('y', '<f8')])
>
> >>> type(b)
>
> <type 'numpy.ndarray'>
>
> >>> type(a)
>
> <class 'numpy.core.records.recarray'>

Yeah, this is on purpose because ndarray classes are actually C 
extensions and they are generally more efficient than Python classes.  

If what you want is a recarray class (Python class), you can always 
create a view as Pierre suggested.  However, this is rarely needed 
because you can access most of the recarray functionality out of the 
ndarray class.

Cheers,

-- 
Francesc Alted



More information about the NumPy-Discussion mailing list