问题
Why does the snipped below give:
"TypeError: Array objects cannot currently deal with void, unicode or object arrays"?
Python 3.8.2, tables 3.6.1, numpy 1.19.1
import numpy as np
import tables as tb
TYPE = np.dtype([
('d', 'f4')
])
with tb.open_file(r'c:\temp\file.h5', mode="a") as h5file:
h5file.create_group(h5file.root, 'grp')
arr = np.array([(1.1)], dtype=TYPE)
h5file.create_array('/grp', str('arr'), arr)
回答1:
File.create_array()
is for homogeneous dtypes (all ints, or all floats, etc). PyTables uses a different object to save mixed dytpes. You need to use File.create_table()
instead. See modified code below (only the last line changed).
TYPE = np.dtype([ ('d', 'f4') ])
with tb.open_file(r'c:\temp\file.h5', mode="a") as h5file:
h5file.create_group(h5file.root, 'grp')
arr = np.array([(1.1)], dtype=TYPE)
h5file.create_table('/grp', str('arr'), arr)
Note: you will get an error with mode='a'
if you run with an existing temp.h5
file from your previous work. This is due to a conflict with group /grp
created the first time.
来源:https://stackoverflow.com/questions/63446020/pytables-create-array-fails-to-save-numpy-array