将HDF文件加载到Python Dask DataFrames列表中

时间:2017-05-07 03:43:56

标签: python-2.7 dataframe hdf5 h5py dask

我有一个HDF5文件,我想加载到Dask DataFrames列表中。我已经使用Dask pipeline approach的缩写版本之后的循环来设置它。这是代码:

import pandas as pd
from dask import compute, delayed
import dask.dataframe as dd
import os, h5py

@delayed
def load(d,k):
    ddf = dd.read_hdf(os.path.join(d,'Cleaned.h5'), key=k)
    return ddf

if __name__ == '__main__':      
    d = 'C:\Users\User\FileD'
    loaded = [load(d,'/DF'+str(i)) for i in range(1,10)]

    ddf_list = compute(*loaded)
    print(ddf_list[0].head(),ddf_list[0].compute().shape)

我收到此错误消息:

C:\Python27\lib\site-packages\tables\group.py:1187: UserWarning: problems loading leaf ``/DF1/table``::

  HDF5 error back trace

  File "..\..\hdf5-1.8.18\src\H5Dio.c", line 173, in H5Dread
    can't read data
  File "..\..\hdf5-1.8.18\src\H5Dio.c", line 543, in H5D__read
    can't initialize I/O info
  File "..\..\hdf5-1.8.18\src\H5Dchunk.c", line 841, in H5D__chunk_io_init
    unable to create file chunk selections
  File "..\..\hdf5-1.8.18\src\H5Dchunk.c", line 1330, in H5D__create_chunk_file_map_hyper
    can't insert chunk into skip list
  File "..\..\hdf5-1.8.18\src\H5SL.c", line 1066, in H5SL_insert
    can't create new skip list node
  File "..\..\hdf5-1.8.18\src\H5SL.c", line 735, in H5SL_insert_common
    can't insert duplicate key

End of HDF5 error back trace

Problems reading the array data.

The leaf will become an ``UnImplemented`` node.
  % (self._g_join(childname), exc))

该消息提到重复的密钥。我迭代了前9个文件以测试代码,并且在循环中,我使用每次迭代来组合我使用dd.read_hdf的不同密钥。在所有迭代中,我保持文件名是相同的 - 只有密钥被更改。

我需要使用dd.concat(list,axis=0,...)来垂直连接文件的内容。我的方法是首先将它们加载到列表中,然后将它们连接起来。

我已经安装了PyTablesh5Py,并且有Dask版本0.14.3+2

使用Pandas 0.20.1,我似乎可以使用它:

for i in range(1,10):
    hdf = pd.HDFStore(os.path.join(d,'Cleaned.h5'),mode='r')
    df = hdf.get('/DF{}' .format(i))
    print df.shape
    hdf.close()

有没有办法将这个HDF5文件加载到Dask DataFrames列表中?或者是否有另一种方法将它们垂直连接在一起?

1 个答案:

答案 0 :(得分:4)

Dask.dataframe已经很懒,所以不需要使用dask.delayed来使它变得更加懒惰。您可以反复拨打dd.read_hdf

ddfs = [dd.read_hdf(os.path.join(d,'Cleaned.h5'), key=k)
        for k in keys]

ddf = dd.concat(ddfs)