python - 如何使用 h5py 为每个 HDF5 列定义单独的数据类型
问题描述
我检查了不同的解决方案,但不明白如何将它们应用于多维数组。准确地说,我的代码生成的数组比它应该的要大,如下图所示:
import h5py
import pandas as pd
import numpy as np
data = [[1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861], [1583663558450195, -7.063664436340332, -6.2776079177856445, -4.206898212432861, -4.206898212432861]]
df = pd.DataFrame(data)
hf = h5py.File('dtype.h5', 'w')
dataTypes = np.dtype([('ts', 'u8'), ('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('temp', 'f4')])
ds = hf.create_dataset('Acceleration', data=df.astype(dataTypes))
我想让它像这样,列分别是 uint64, 4x float32:
ts x y z temp
0 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
1 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
2 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
3 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
4 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
5 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
6 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
7 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
8 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
9 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
解决方案
你的df
:
In [370]: df
Out[370]:
0 1 2 3 4
0 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
1 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
2 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
3 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
...
df.astype(dataTypes)
给了我一个TypeError
(我pd
的不是最新的)。
In [373]: df.to_records()
Out[373]:
rec.array([(0, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(1, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(2, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(3, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(4, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(5, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(6, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(7, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(8, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821),
(9, 1583663558450195, -7.06366444, -6.27760792, -4.20689821, -4.20689821)],
dtype=[('index', '<i8'), ('0', '<i8'), ('1', '<f8'), ('2', '<f8'), ('3', '<f8'), ('4', '<f8')])
该数组应保存为h5py
.
to_records
具有可能创建更接近您的dataTypes
. 我会让你探索这些。
但是通过最新的重组 a recfunctions
,我们可以制作一个结构化数组:
In [385]: import numpy.lib.recfunctions as rf
In [386]: rf.unstructured_to_structured(np.array(data), dataTypes)
Out[386]:
array([(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898),
(1583663558450195, -7.0636644, -6.277608, -4.206898, -4.206898)],
dtype=[('ts', '<u8'), ('x', '<f4'), ('y', '<f4'), ('z', '<f4'), ('temp', '<f4')])
np.array(data)
是 (10,5) 浮点数组。
In [388]: pd.DataFrame(_386)
Out[388]:
ts x y z temp
0 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
1 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
2 1583663558450195 -7.063664 -6.277608 -4.206898 -4.206898
...
推荐阅读
- reactjs - 为什么我看不到 currentUser 使用带有 react native 的 firebase?
- javascript - 无法理解 JSON.parse 的错误
- flutter - 如何用颤动动画实现飞车动画颤动
- ruby-on-rails - 如何在haml代码中删除一行表?
- linux - Linux命令导航同一目录中的槽文件夹
- java - 我需要 Docker 容器中的任何服务器来运行我的 Java 应用程序吗?
- java - Spring 不支持请求方法“POST”
- java - 如何在 Android 上正确使用 CRC32
- c# - 通过 LINQ 对列表中的特定值进行排序
- python - 找到一个子字符串并打印它直到特定字符