PyTables create_array fails to save numpy array

假如想象 提交于 2021-01-29 17:27:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!