[Numpy-discussion] Empty record arrays

Sameer DCosta sameerslists@gmail....
Mon Mar 5 18:26:21 CST 2007


Hi,

I am trying to create record arrays using the numpy.rec.fromrecords.
This function works just fine, however I am having a little trouble
using it to create empty record arrays. If the array I start off with
is empty and I have provided a dtype (or the names and the formats),
then I expect rec.fromrecords to return an empty record array. However
I just get an error.

I am using numpy version 1.0, but a quick glance at
numpy/core/records.py in the svn repository seems to indicate that I
will have this problem even with the svn version. Any thoughts? Thanks
in advance.

Sameer


# *********  code snippet ***********
from numpy import *
recs = [( 'Bill' , 31, 260.0), ( 'Fred' , 15, 145.0)]
dt = [("name", "S30"), ("age", "i2"), ("weight", "f4")]

# Using populated array
r = rec.fromrecords(recs, dtype=dt)
print r.name

try:
    # Using empty array gives an error
    r = rec.fromrecords([], dtype=dt)
    print r
except IndexError:
    print "Index Error using empty array"

# workaround
r = array([], dtype=dt).view(recarray)
print r.name


More information about the Numpy-discussion mailing list