Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将HDF5转换为拼花地板而无需加载到内存中_Python_Pandas_Hdf5_Parquet_Hdf - Fatal编程技术网

Python 将HDF5转换为拼花地板而无需加载到内存中

Python 将HDF5转换为拼花地板而无需加载到内存中,python,pandas,hdf5,parquet,hdf,Python,Pandas,Hdf5,Parquet,Hdf,我有一个大数据集(~600GB),存储为HDF5格式。由于它太大,无法放入内存,我想将其转换为拼花格式,并使用pySpark执行一些基本数据预处理(规范化、查找相关矩阵等)。但是,我不确定如何在不将整个数据集加载到内存的情况下将其转换为拼花地板 我看了一下要点:,但似乎整个数据集都被读入了内存 我想到的一件事是将HDF5文件分块读取并以增量方式保存到拼花地板文件中: test_store = pd.HDFStore('/path/to/myHDFfile.h5') nrows = test_st

我有一个大数据集(~600GB),存储为HDF5格式。由于它太大,无法放入内存,我想将其转换为拼花格式,并使用pySpark执行一些基本数据预处理(规范化、查找相关矩阵等)。但是,我不确定如何在不将整个数据集加载到内存的情况下将其转换为拼花地板

我看了一下要点:,但似乎整个数据集都被读入了内存

我想到的一件事是将HDF5文件分块读取并以增量方式保存到拼花地板文件中:

test_store = pd.HDFStore('/path/to/myHDFfile.h5')
nrows = test_store.get_storer('df').nrows
chunksize = N
for i in range(nrows//chunksize + 1):
    # convert_to_Parquet() ...
但是我找不到任何文档可以让我逐步建立拼花地板文件。如有任何进一步阅读的链接,我们将不胜感激。

您可以使用此链接

import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq


def convert_hdf5_to_parquet(h5_file, parquet_file, chunksize=100000):

    stream = pd.read_hdf(h5_file, chunksize=chunksize)

    for i, chunk in enumerate(stream):
        print("Chunk {}".format(i))

        if i == 0:
            # Infer schema and open parquet file on first chunk
            parquet_schema = pa.Table.from_pandas(df=chunk).schema
            parquet_writer = pq.ParquetWriter(parquet_file, parquet_schema, compression='snappy')

        table = pa.Table.from_pandas(chunk, schema=parquet_schema)
        parquet_writer.write_table(table)

    parquet_writer.close()

感谢您的回答,我尝试从CLI调用下面的py脚本,但它既没有显示任何错误,也看不到转换后的拼花地板文件

h5文件也不是空的

作为pd进口熊猫 将pyarrow作为pa导入 导入pyarrow.parquet作为pq

h5_file=“C:\Users…\tall.h5” 拼花\u file=“C:\Users…\my.parquet”

def将_hdf5_转换为_拼花地板(h5_文件,拼花地板文件,chunksize=100000):


请注意,拼花地板数据集是由许多文件组成的。它们不需要包含单个大文件,因此分块方法是一种很好的方法。它可能有1000个文件,这很好。尝试这个,我得到了“KeyError:‘index_level_0’”,试图将“preserve_index=False”添加到
from_pandas
方法中,但没有用。啊,我需要在第一次调用
表时设置preserve_index=False。同样,从\u pandas
中,这样架构就正确设置了!pandas
read\u hdf
方法需要一个包含单个表的hdf5文件。对于包含自定义层次结构中多个表的hdf5文件,需要编写自定义代码来提取每个表。两个可能对此有用的Python包是和。此外,这可能是一个新问题,而不是现有问题的答案。谢谢您的回复!但是我的hdf5文件包含n个表,我不想在代码中明确提到所有表。每个文件的“N”更改。当我尝试wity h5py时,我相信——它的任务是指定名称。请提出其他建议。让我也开始一个新的线程相同。
stream = pd.read_hdf(h5_file, chunksize=chunksize)

for i, chunk in enumerate(stream):
    print("Chunk {}".format(i))
    print(chunk.head())

    if i == 0:
        # Infer schema and open parquet file on first chunk
        parquet_schema = pa.Table.from_pandas(df=chunk).schema
        parquet_writer = pq.ParquetWriter(parquet_file, parquet_schema, compression='snappy')

    table = pa.Table.from_pandas(chunk, schema=parquet_schema)
    parquet_writer.write_table(table)
parquet_writer.close()