【问题标题】:python pandas HDF5Store append new dataframe with long string columns failspython pandas HDF5Store 附加带有长字符串列的新数据帧失败
【发布时间】:2016-06-19 07:02:10
【问题描述】:

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

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

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()

->

%time load_data(publications_path)

我得到的 ValueError 是:

...

ValueError: Trying to store a string with len [5761] in [values_block_0]
column but this column has a limit of [4430]!
Consider using min_itemsize to preset the sizes on these columns

我尝试了所有选项,浏览了此任务所需的所有文档,并尝试了我在 Internet 上看到的所有技巧。然而,不知道为什么会这样。

我使用 pandas 版本:0.17.0

非常感谢您的帮助!

【问题讨论】:

    标签: python pandas hdf5


    【解决方案1】:

    你看过这篇文章吗? stackoverflow

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

    将“字符串”更改为您的类型。

    【讨论】:

    • 感谢您的回答。我重构了我的代码,最终我设法克服了这个错误。我现在面临一个关于此代码的新问题,还找不到任何解决方案。非常感谢您的帮助:) [stackoverflow.com/questions/35818844/…
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2012-01-10
    • 2022-10-14
    • 2018-07-29
    • 2015-12-21
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多