使用 Python 的 HDF 文件中的数据缺失
Data in HDF file using Python missing
我正在尝试读取 hdf 文件,但没有显示组。我尝试了几种使用表和 h5py 的不同方法,但都无法在文件中显示组。我检查了一下,文件是 'Hierarchical Data Format (version 5) data'(见更新)。文件信息here供参考
可以找到示例数据here
import h5py
import tables as tb
hdffile = "TRMM_LIS_SC.04.1_2010.260.73132"
使用 h5py:
f = h5py.File(hdffile,'w')
print(f)
输出:
< HDF5 file "TRMM_LIS_SC.04.1_2010.260.73132" (mode r+) >
[]
使用表格:
fi=tb.openFile(hdffile,'r')
print(fi)
输出:
TRMM_LIS_SC.04.1_2010.260.73132 (File) ''
Last modif.: 'Wed Aug 10 18:41:44 2016'
Object Tree:
/ (RootGroup) ''
Closing remaining open files:TRMM_LIS_SC.04.1_2010.260.73132...done
更新
h5py.File(hdffile,'w') overwrote the file and emptied it.
现在我的问题是如何将 hdf 版本 4 文件读入 python 因为 h5py 和表都不起作用?
尝试使用 pandas:
import pandas as pd
f = pd.read_hdf(C:/path/to/file)
See Pandas HDF documentation here.
这应该将任何 hdf 文件作为数据帧读取,然后您可以对其进行操作。
文件有多大?我认为 h5py.File(hdffile,'w')
会覆盖它,所以它是空的。使用h5py.File(hdffile,'r')
阅读。
我没有足够的业力来回复@Luke H 的回答,但将其读入 pandas 可能不是一个好主意。 Pandas hdf5 使用 pytables,这是使用 hdf5 的一种 "opinionated" 方式。这意味着它存储额外的元数据(例如索引)。所以我只会使用 pytables 来读取文件,如果它是用 pytables 制作的。
更新:
我建议您首先 convert 将 HDF 版本 4 文件转换为 HDF5 / h5 文件,因为所有现代库/模块都使用 HDF 版本 5...
旧答案:
这样试试:
store = pd.HDFStore(filename)
print(store)
这应该会打印出有关 HDF 文件的详细信息,包括存储的密钥、存储的 DF 的长度等。
演示:
In [18]: fn = r'C:\Temp\a.h5'
In [19]: store = pd.HDFStore(fn)
In [20]: print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: C:\Temp\a.h5
/df_dc frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/df_no_dc frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
现在您可以使用上面输出中的键读取数据帧:
In [21]: df = store.select('df_dc')
In [22]: df
Out[22]:
a b c
0 92 80 86
1 27 49 62
2 55 64 60
3 31 66 3
4 37 75 81
5 49 69 87
6 59 0 87
7 69 91 39
8 93 75 31
9 21 15 7
我正在尝试读取 hdf 文件,但没有显示组。我尝试了几种使用表和 h5py 的不同方法,但都无法在文件中显示组。我检查了一下,文件是 'Hierarchical Data Format (version 5) data'(见更新)。文件信息here供参考
可以找到示例数据here
import h5py
import tables as tb
hdffile = "TRMM_LIS_SC.04.1_2010.260.73132"
使用 h5py:
f = h5py.File(hdffile,'w')
print(f)
输出:
< HDF5 file "TRMM_LIS_SC.04.1_2010.260.73132" (mode r+) >
[]
使用表格:
fi=tb.openFile(hdffile,'r')
print(fi)
输出:
TRMM_LIS_SC.04.1_2010.260.73132 (File) ''
Last modif.: 'Wed Aug 10 18:41:44 2016'
Object Tree:
/ (RootGroup) ''
Closing remaining open files:TRMM_LIS_SC.04.1_2010.260.73132...done
更新
h5py.File(hdffile,'w') overwrote the file and emptied it.
现在我的问题是如何将 hdf 版本 4 文件读入 python 因为 h5py 和表都不起作用?
尝试使用 pandas:
import pandas as pd
f = pd.read_hdf(C:/path/to/file)
See Pandas HDF documentation here.
这应该将任何 hdf 文件作为数据帧读取,然后您可以对其进行操作。
文件有多大?我认为 h5py.File(hdffile,'w')
会覆盖它,所以它是空的。使用h5py.File(hdffile,'r')
阅读。
我没有足够的业力来回复@Luke H 的回答,但将其读入 pandas 可能不是一个好主意。 Pandas hdf5 使用 pytables,这是使用 hdf5 的一种 "opinionated" 方式。这意味着它存储额外的元数据(例如索引)。所以我只会使用 pytables 来读取文件,如果它是用 pytables 制作的。
更新:
我建议您首先 convert 将 HDF 版本 4 文件转换为 HDF5 / h5 文件,因为所有现代库/模块都使用 HDF 版本 5...
旧答案:
这样试试:
store = pd.HDFStore(filename)
print(store)
这应该会打印出有关 HDF 文件的详细信息,包括存储的密钥、存储的 DF 的长度等。
演示:
In [18]: fn = r'C:\Temp\a.h5'
In [19]: store = pd.HDFStore(fn)
In [20]: print(store)
<class 'pandas.io.pytables.HDFStore'>
File path: C:\Temp\a.h5
/df_dc frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index],dc->[a,b,c])
/df_no_dc frame_table (typ->appendable,nrows->10,ncols->3,indexers->[index])
现在您可以使用上面输出中的键读取数据帧:
In [21]: df = store.select('df_dc')
In [22]: df
Out[22]:
a b c
0 92 80 86
1 27 49 62
2 55 64 60
3 31 66 3
4 37 75 81
5 49 69 87
6 59 0 87
7 69 91 39
8 93 75 31
9 21 15 7