[Numpy-discussion] array with object

Christopher Barker Chris.Barker at noaa.gov
Wed Nov 29 14:04:45 CST 2006


Lionel Roubeyrie wrote:
> Hi all,
> I don't understand why can't I do that:
> b=array([(datetime.datetime(2006,11,29),'2','3'),
> (datetime.datetime(2006,11,30),'5','6')], dtype=[('Dates', 'object'),
> ('HUM', 'float32'), ('TEM', 'float32')])
> ValueError: tried to set void-array with object members using buffer.
> Thanks

I'm not sure why this fails, but it does work if you create an empty 
array, then set the values individually:

 >>> a = N.empty((2,),dtype = [('Dates', 'object'),('HUM', 'float32'), 
('TEM', 'float32')])
 >>> a
array([(None, 0.0, 0.0), (None, 0.0, 0.0)],
       dtype=[('Dates', '|O4'), ('HUM', '>f4'), ('TEM', '>f4')])

 >>> a[0]['Dates'] = datetime.datetime(2006,11,29)
 >>> a[0]['HUM'] = 2
 >>> a[0]['TEM'] = 3

 >>> a
array([(datetime.datetime(2006, 11, 29, 0, 0), 2.0, 3.0), (None, 0.0, 
0.0)], dtype=[('Dates', '|O4'), ('HUM', '>f4'), ('TEM', '>f4')])

Both recarrays and working the object data type are tricky.

-Chris


-- 
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