pyarrow通过列索引或顺序读取拼花地板?

问题描述

是否有一种解决方法,可以通过列索引而不是列名来选择性地读取镶木地板文件

文档显示通过列名称进行阅读:

pq.read_table('example.parquet',columns=['one','three'])

我正在寻找的东西是这样的:

pq.read_table('example.parquet',columns=[0,2])

相似的问题:Pandas Read/Write Parquet Data using Column Index


尝试更新

这是多余的,我最好用pandas或numpy在内存中删除列。

desired_cols = [0,2]

pat = pq.read_table('file.parquet.gzip')

cols_names = pat.column_names

del pat

desired_cols = [cols_names[c] for c in desired_cols]

pq.read_table('file.parquet.gzip',columns=desired_cols)

"""
pyarrow.Table
anzsic06: string
year: int64
"""

解决方法

您可以阅读ParquetFile,它为您提供了不加载基础数据的架构。从那里,您可以根据索引找出想要的列的名称,并仅加载以下列:

# Load meta data & guess column names:
pq_file = pq.ParquetFile('file.parquet')
column_indices = [1,2]
column_names = [pq_file.schema[i].name for i in column_indices]

# Load the actual data:
pq.read_table('file.parquet',columns=column_names)

请参见http://arrow.apache.org/docs/python/parquet.html#inspecting-the-parquet-file-metadata