I am trying to append a date-time field (datetime64) to an existing recarray - without much success. I can create the datetime field, but when I attempt to append it to the record array I get the error:
ValueError: Error parsing datetime string "?" at position 0
However, if I cast the data as int64 I can add it in that format without problem. (code shown below)
Anyone know why this does not work?
(my ultimate goal is to write the recarray to a netcdf file, so an appropriate datetime format with that goal in mind would also be helpful)
I am using python 2.7.6.1, numpy 1.8.1
Thanks, Rob
import numpy as np
import numpy.lib.recfunctions as rf
# ----- make a recarray ---------
dummy = np.arange(0,10)
datarray = np.core.records.fromarrays([dummy,dummy,dummy],names='a,b,c')
# ----- make some time data using datetime64 ---------
sec = np.arange(0,10)*1000
millisec = np.arange(0,10)
mytime = sec + millisec
mytime64 = mytime.astype('timedelta64[ms]')
basetime = np.datetime64('1990-01-01')
mydatetime = mytime64+basetime
# ----- convert time data to int64 ---------
idatetime = mydatetime.astype('int64');
#------ try and append to recarray ---------
# this works
datarray = rf.append_fields(datarray, 'iDateTime', data=idatetime)
# this doesnt
datarray = rf.append_fields(datarray, 'DateTime', data=mydatetime)
See Question&Answers more detail:os