我认为您使用NaN 是完全正确的。
我会设置一个fill value 并使用它,这样做我会使用NaN 或None。
#!/usr/bin/env python
import numpy as np
import h5py as h5
f = h5.File('test.h5','w')
ctype = np.dtype([('time','i'),
('x1','f8'),('y1','f8'),
('x2','f8'),('y2','f8')])
d = f.create_dataset('test', (5,), dtype=ctype)
d.set_fill_value = np.nan
data = np.array([(0, 2.0, 1.0, 2.0, 3.0),
(1, 2.1, 1.0, 2.3, 3.1),
(2, 2.4, 1.4, np.nan, np.nan),
(3, 2.2, 1.5, 2.4, 3.1),
(4, np.nan, np.nan, 2.3, 3.2)],
dtype = ctype)
d[...] = data
f.close()
然后如果我们运行它并查看它生成的文件。
localhost ~$ ./test.py
localhost ~$ h5dump test.h5
h5dump test.h5
HDF5 "test.h5" {
GROUP "/" {
DATASET "test" {
DATATYPE H5T_COMPOUND {
H5T_STD_I32LE "time";
H5T_IEEE_F64LE "x1";
H5T_IEEE_F64LE "y1";
H5T_IEEE_F64LE "x2";
H5T_IEEE_F64LE "y2";
}
DATASPACE SIMPLE { ( 5 ) / ( 5 ) }
DATA {
(0): {
0,
2,
1,
2,
3
},
(1): {
1,
2.1,
1,
2.3,
3.1
},
(2): {
2,
2.4,
1.4,
nan,
nan
},
(3): {
3,
2.2,
1.5,
2.4,
3.1
},
(4): {
4,
nan,
nan,
2.3,
3.2
}
}
}
}
}
当然,您不必使用复合数据类型,我只是这样做了,因为它在您的上下文中是有意义的。