[Numpy-discussion] Empty record arrays
Christopher Barker
Chris.Barker at noaa.gov
Mon Mar 5 19:54:12 EST 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)
>>> a
array([],
dtype=[('name', '|S30'), ('age', '>i2'), ('weight', '>f4')])
-Chris
Sameer DCosta wrote:
> 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
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
--
Christopher Barker, Ph.D.
Oceanographer
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
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list