PyTables create_array 无法保存 numpy 数组 [英] PyTables create_array fails to save numpy array

查看:60
本文介绍了PyTables create_array 无法保存 numpy 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么下面的截图给出:

Why does the snipped below give:

类型错误:数组对象当前无法处理 void、unicode 或对象数组"?

Python 3.8.2,表 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)

推荐答案

File.create_array() 适用于同类数据类型(所有整数或所有浮点数等).PyTables 使用不同的对象来保存混合的 dytpes.您需要使用 File.create_table() 代替.请参阅下面的修改代码(仅更改了最后一行).

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)

注意:如果您使用之前工作中的现有 temp.h5 文件运行,您将收到 mode='a' 错误.这是由于与第一次创建的组 /grp 发生冲突.

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.

这篇关于PyTables create_array 无法保存 numpy 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆