[Numpy-discussion] Empty record arrays
Mon Mar 5 18:54:12 CST 2007
empty() works too, but why would you want an empty array?
>>> import numpy as N
>>> dt = [("name", "S30"), ("age", "i2"), ("weight", "f4")]
>>> a = N.empty((0,), dtype=dt)
dtype=[('name', '|S30'), ('age', '>i2'), ('weight', '>f4')])
Sameer DCosta wrote:
> 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.
> # ********* 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
> # 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
> Numpy-discussion mailing list
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
More information about the Numpy-discussion