Search code examples
pythondatetimenumpypytables

mismatch between the number of fields and the number of arrays


I want to append list to a table in h5. when append the data directly, it was done;

ts=[(datetime.date(2017, 5, 30), 233, 856, 0.2355, 1.17692), (datetime.date(2017, 5, 30), 192, 518, 1.27893, -0.60389)] tab2.append(rows=ts)

while append the data from source,

 tab2.append(rows=tarray)

it reports error

"rows parameter cannot be converted into a recarray object compliant with table '/test (Table(4,)) 'TestTables''. The error was: <mismatch between the number of fields and the number of arrays>"

while the tarray is got from the following expression.

ran_int=np.random.randint(0,high=1000,size=(nrows,2))
ran_flo=np.random.standard_normal(size=(nrows,2)).round(5)

dty=np.dtype([('Date','M8[D]'),('No1','<i4'),('No2','<i4'),
                             ('No3','<f8'),('No4','<f8')])

sarray=np.zeros(len(ran_int),dtype=dty)

sarray['Date']=datetime.datetime.now().date()
sarray['No1']=ran_int[:,0]
sarray['No2']=ran_int[:,1]
sarray['No3']=ran_flo[:,0]
sarray['No4']=ran_flo[:,1]

tarray=list(sarray)

I printed both ts, tarray and their types, and got the same information as following.

[(datetime.date(2017, 5, 30), 866, 536, -0.83165, 0.50131), (datetime.date(2017, 5, 30), 14, 117, 2.11287, -1.09029)] <class 'list'> [(datetime.date(2017, 5, 30), 233, 856, 0.2355, 1.17692), (datetime.date(2017, 5, 30), 192, 518, 1.27893, -0.60389)] <class 'list'>

It's not the problem of datetime. I try to change the dty

dty=np.dtype([('Date','S26'),('No1','<i4'),('No2','<i4'),
                         ('No3','<f8'),('No4','<f8')])

and

sarray['Date']=datetime.datetime.now().strftime('%Y-%m-%d')

In this situation, I got the same result. it is the same as above.


Solution

  • got it, it's the problem of sarray.

    tarray = []
    for s in sarray:
        s=tuple(s)
        tarray.append(s)