首页 > 解决方案 > 如何使用 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

标签: pythonhdf5h5py

解决方案


你的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
 ...

推荐阅读