将数据集转换为 HDF5 数据集

Converting a dataset into an HDF5 dataset

我有一个数据集,我想将其转换为 HDF5 格式。 它是来自 NOAA 的数据集。 目录结构类似于:

NOAA
├── code
├── ghcnd_all
├── ghcnd_all.tar.gz
├── ghcnd-stations.txt
├── ghcnd-version.txt
├── readme.txt
└── status.txt

我正在与 pandas 一起进行数据分析。我有兴趣这样做的主要原因是为了节省 space,数据集是 ~25Gb。

如何将此数据集转换为单个 .hdf5 文件?

HDF5 中的数据存储在数据集中,这些数据集可能是多维的,最多 32 个维度,每个维度最多有一个无符号的 64 位整数长度(列数),并且包含任意大小的数据类型,包括复合数据类型单个数据集的上限超过 16 艾字节。数据集旨在保存结构化数据,例如 numpy 数组、pandas DataFrame、图像和电子表格。我还没有找到任何方法可以直接将纯文本或 tar.gz 文件放入 HDF5。但是,使用 Python 您可以将文件读入字符串并将其放入数据集中,如 Strings in HDF5 所示。除了数据集之外,组是 HDF5 中的另一个主要对象类型,是数据集和其他组的容器。数据集和组类似于文件和目录(或文件夹),并为分层格式(如 Unix 文件系统)提供基础,其中可以使用以 / 开头的路径名访问对象。 HDF5 文件是可能包含多个数据集和组的容器,并且没有大小限制。

为了更好地了解 HDF5 是什么,我建议从 Wes McKinney 的 HDF5 Downloads, installing it all and then going through Learning HDF5 with HDFView, which can be done within 30 minutes. HDFView is a Java GUI that makes it easy to interact with HDF5, however you cannot simply drag and drop files into it but file data can be imported into a dataset. It is very easy to create HDF5 files and add DataFrames to them with pandas and that's a good method for putting data into a HDF5 file. Below is a demonstration of that. For more information about HDF5 you might take a look at other tutorials listed on HDF5 Tutorials, HDF5 Python Examples by API, Additional HDF5 Python Examples and the Python h5py package documentation at HDF5 for Python. For more information about pandas, 10 Minutes to pandas is a good place to start, followed by pandas Cookbook for a series of code examples and Python for Data Analysis 下载它和随附的实用程序以及 HDFView,这是自他发明和开发以来关于 pandas 的最佳教程开发了其中的大部分内容,并且是一位出色的作者。

这是一个使用 pandas 创建 HDF5 文件、将 DataFrame 加载到其中并检索它的副本并将其存储在另一个变量中的示例:

In [193]: import pandas as pd

In [194]: frame = pd.read_csv('test.csv')

In [195]: frame
Out[195]: 
   a   b   c   d message
0  1   2   3   4     one
1  5   6   7   8     two
2  9  10  11  12   three

In [196]: type(frame)
Out[196]: pandas.core.frame.DataFrame

In [197]: hdf5store = pd.HDFStore('mydata.h5')

In [198] %ls mydata.h5
 Volume in drive C is OS
 Volume Serial Number is 5B75-665D

 Directory of C:\Users\tn\Documents\python\pydata

09/02/2015  12:41 PM                 0 mydata.h5
               1 File(s)              0 bytes
               0 Dir(s)  300,651,331,584 bytes free

In [199]: hd5store['frame'] = frame

In [200]: hdf5store
Out[200]: 
<class 'pandas.io.pytables.HDFStore'>
File path: mydata.h5
/frame            frame        (shape->[3,5])

In [201]: list(hdf5store.items())
Out[201]: 
[('/frame', /frame (Group) ''
    children := ['block0_values' (Array), 'block0_items' (Array), 'axis1' (Array), 'block1_items' (Array), 'axis0' (Array), 'block1_values' (VLArray)])]

In [202]: hdf5store.close()

现在演示从 mydata.h5 检索帧的能力:

In [203]: hdf5store2 = pd.HDFStore('mydata.h5')

In [204]: list(hdf5store2.items())
Out[204]: 
[('/frame', /frame (Group) ''
    children := ['block0_values' (Array), 'block0_items' (Array), 'axis1' (Array), 'block1_items' (Array), 'axis0' (Array), 'block1_values' (VLArray)])]

In [205]: framecopy = hdf5store2['frame']

In [206]: framecopy
Out[206]: 
   a   b   c   d message
0  1   2   3   4     one
1  5   6   7   8     two
2  9  10  11  12   three

In [207]: framecopy == frame
Out[207]: 
      a     b     c     d message
0  True  True  True  True    True
1  True  True  True  True    True
2  True  True  True  True    True

In [208]: hdf5store2.close()