numpy – insert a tuple or a list into numpy array items –

Question:

import tables, h5py
import math, sys, time
import numpy as np
import numpy.lib.recfunctions as rf
v_dt = np.dtype([ ('EID','i8'), ('CID','i8'), ('CTYPE','S4'), ('NODEF','i8'), ('f1', '<i8', (5,) )  ])
print (v_dt)
value_list = [ ( 1, 0, 'GRID', 10, (1,2,3,4,5) ) ]
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
v_rec_arr = np.rec.array(np_value_list, dtype=v_dt)

gives me an error message:

VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences 
(which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) 
is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the 
ndarray.
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
Traceback (most recent call last):
File "digimat_to_hdf5.py", line 13, in <module>
np_value_list = rf.unstructured_to_structured(np.array(value_list), v_dt)
File "<__array_function__ internals>", line 6, in unstructured_to_structured
File "C:Userslutz.peschlowAppDataRoamingPythonPython37site-packagesnumpy
librecfunctions.py", line 1074, in unstructured_to_structured
raise ValueError('The length of the last dimension of arr must '
ValueError: The length of the last dimension of arr must be equal to the number of fields 
in dtype*

And I do not know, how to setup now the creation of the np array,
in the past I did not used a tuple as part of the data set,

Can you help me please how can I create a numpy record array for that data?

Asked By: Lutz_P

||

Answers:

I found out, that several newer Python versions were not able to do that way
that worked in earlier Python versions 3.4 and I have the feeling, that the newer the
Python version 3.7 or 3.9, the more problems with tables or h5py,

the way as described above did not work anymore with newer versions and as workaround I used following way to create datasets for writing hdf5:

create a separate pre-definition of datablock:

class INDEX_DISP(IsDescription):
  DOMAIN_ID = Int64Col(pos = 0)
  POSITION = Int64Col(pos = 1)
  LENGTH = Int64Col(pos = 2)

open hdf5, assign values and close hdf5:

h5 = open_file('t.h5', mode = 'w')
index_disp = h5.create_table('/INDEX/NODAL','DISP',INDEX_DISP,createparents = True)
row = index_disp.row
for i in range(1,2):
    row['DOMAIN_ID'] = 1
    row['POSITION'] = 0
    row['LENGTH'] = 24
    row.append()
index_disp.flush() 
h5.close()
ยดยดยด

Answered By: Lutz_P
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.