python pandas HDF5Store 附加带有长字符串列的新数据框失败

标签 python pandas hdf5

对于给定的路径,我在其中处理许多千兆字节的文件,并为每个处理过的文件生成数据帧。 对于每个 yield 数据帧,其中包括两个不同大小的字符串列,我想使用非常高效的 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 ver: 0.17.0

非常感谢您的帮助!

最佳答案

你看过这篇文章吗? stackoverflow

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

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

关于python pandas HDF5Store 附加带有长字符串列的新数据框失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35814769/

相关文章:

Python:在数据框中将 timedelta 转换为 int

python - 如何将 SQL Oracle 数据库转换为 Pandas DataFrame?

python - 如何通过 numpy loadtxt 获取标签?

python - 散列两个序列中的值时如何处理被零除错误

python - 过滤包含序列中特定值的 df - pandas

hdf5 - h5py 是否能够自动将 python 字典转换为 hdf5 组?

python - 打开损坏的 PyTables HDF5 文件

c++ - hdf5/c++ 中的测试组存在

python - 从同一网络上的另一台计算机访问 Django 网站

python - 是否可以使用 Python 在 pool.map() 中使用 "static"对 self 的引用?