python pandas HDF5Store追加新的数据帧与长字符串列失败

时间:2016-03-05 13:31:20

标签: python pandas hdf5

对于给定的路径,我处理内部的许多GigaBytes文件,并为每个处理过的文件生成数据帧。 对于每个yield的数据帧,包括两个不同大小的字符串列,我想使用非常高效的HDF5格式将它们转储到磁盘。在调用HDFStore.append过程时,会在第4次或第5次迭代时引发错误。

我使用以下例程(简化)来构建数据帧:

-- hostfile --

host1   slots=4

host2   slots=4

host3   slots=4

'--------------


.

然后我使用以下方法将这些数据帧写入磁盘:

def build_data_frames(path):
    data = df({'headline': [], 
           'content': [], 
           'publication': [],
           'file_ref': []},
           columns=['publication','file_ref','headline','content'])
    for curdir, subdirs, filenames in os.walk(path):
        for file in filenames:
            if (zipfile.is_zipfile(os.path.join(curdir, file))):
                with zf(os.path.join(curdir, file), 'r') as arch:
                    for arch_file_name in arch.namelist():
                        if re.search('A[r|d]\d+.xml', arch_file_name) is not None:
                            xml_file_ref = arch.open(arch_file_name, 'r')
                            xml_file = xml_file_ref.read()
                            metadata = XML2MetaData(xml_file)
                            headlineTokens, contentTokens = XML2TokensParser(xml_file)

                            rows= [{'headline': " ".join(headlineTokens), 
                                    'content': " ".join(contentTokens)}]
                            rows[0].update(metadata)
                            data = data.append(df(rows,
                                                  columns=['publication',
                                                           'file_ref',
                                                           'headline',
                                                           'content']),
                                                    ignore_index=True)
                    arch.close()
                    yield data

- >

def extract_data(path):
    hdf_fname = extract_name(path)
    hdf_fname += ".h5"
    data_store = HDFStore(hdf_fname)

    for dataframe in build_data_frames(path):                
        data_store.append('df', dataframe, data_columns=True)
        ## passing min_itemsize doesn't work either
        ## data_store.append('df', dataframe, min_itemsize=8000)

        ## trying the "alternative" command didn't help
        ## dataframe.to_hdf(hdf_fname, 'df', format='table', append=True,
        ##                  min_itemsize=80000)
    data_store.close()

我得到的ValueError是:

...

%time load_data(publications_path)

我尝试了所有选项,完成了此任务所需的所有文档,并尝试了我在互联网上看到的所有技巧。然而,不知道它为什么会发生。

我使用pandas ver:0.17.0

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

你看过这篇文章吗? stackoverflow

data_store.append('df',dataframe,min_itemsize={ 'string' : 5761 })

将'string'更改为您的类型。