【问题标题】:How to read parquet file with a condition using pyarrow in Python如何在 Python 中使用 pyarrow 读取带有条件的镶木地板文件
【发布时间】:2018-07-20 18:05:03
【问题描述】:

我从数据库中创建了一个包含三列(id、作者、标题)的 parquet 文件,并且想要读取带有条件(title='Learn Python')的 parquet 文件。 下面提到的是我用于此 POC 的 python 代码。

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

def write_to_parquet(df, out_path, compression='SNAPPY'):
arrow_table = pa.Table.from_pandas(df)
if compression == 'UNCOMPRESSED':
    compression = None
pq.write_table(arrow_table, out_path, use_dictionary=False,
               compression=compression)

def read_pyarrow(path, nthreads=1):
return pq.read_table(path, nthreads=nthreads).to_pandas()


path = './test.parquet'
sql = "SELECT * FROM [dbo].[Book] (NOLOCK)"

conn = pyodbc.connect(r'Driver={SQL 
Server};Server=.;Database=APP_BBG_RECN;Trusted_Connection=yes;')
df = pd.io.sql.read_sql(sql, conn)

write_to_parquet(df, path)

df1 = read_pyarrow(path)

如何在 read_pyarrow 方法中添加条件(title='Learn Python')?

【问题讨论】:

    标签: python filter conditional-statements parquet pyarrow


    【解决方案1】:

    过滤器现在可用 read_table

    table = pq.read_table(
            df, filters=[("title", "in", {'Learn Python'}), 
                         ("year", ">=", 1950)]
        )
     
    

    【讨论】:

      【解决方案2】:

      目前尚不支持。我们打算在未来开发此功能。我建议在从箭头表转换后使用 pandas 进行过滤。

      【讨论】:

      猜你喜欢
      • 2018-05-06
      • 2017-12-18
      • 2019-10-27
      • 1970-01-01
      • 2021-12-06
      • 2020-03-05
      • 2018-04-17
      • 2021-08-31
      • 2018-03-29
      相关资源
      最近更新 更多