Python : Arrow、Pyarrow库、以及与Julia互读

79 篇文章 6 订阅

test.csv 样例:
在这里插入图片描述
一、从csv =>table=>生成arrow文件

import pyarrow as pa
from pyarrow import csv
csv_path = 'C:\\Users\\songroom\\Desktop\\test.csv'
table = csv.read_csv(csv_path)
#df = table.to_pandas()
#table = pa.Table.from_pandas(df)
path = 'C:\\Users\\songroom\\Desktop\\py.arrow'
writer = pa.RecordBatchFileWriter(path, table.schema)
writer.write_table(table)
writer.close()

二、读出arrow文件,并转成DataFrame

用下例方式,读出pyarrow写的py.arrow文件:


import pyarrow as pa;
path = 'C:\\Users\\songroom\\Desktop\\py.arrow'
df = pa.ipc.open_file(path).read_pandas()
print(df)

三、julia与python arrow文件的互读

1、pyarrrow: 读julia生成的一个test.arrow文件。

值得注意的是,以下两种方式在与julia文件交互上有较大不同:

# 不能读出julia对应的test.arrow文件
def read_arrow_to_df_julia_not_ok(path):
    df = pa.ipc.open_file(path).read_pandas()
    return df
# 可以读出julia对应的test.arrow文件
def read_arrow_to_df_julia_ok(path):
    with open(path,"rb") as f:
        r = pa.ipc.RecordBatchStreamReader(f)
        df = r.read_pandas()
    return df
>>> path = 'C:\\Users\\songroom\\Desktop\\test.arrow'
>>> t0 = t.time()
>>> df = read_arrow_to_df_julia_ok(path)
>>> t1 = t.time()
>>> print("read julia arrow file cost time: ",t1-t0)
read julia arrow file cost time:  0.23099970817565918

但好象速度很慢!0.23s.

2、julia:读出pyarrow库生成的py.arrow文件

代码如下:

using DataFrames;
using Arrow

arrow_path = "C:\\Users\\songroom\\Desktop\\py.arrow"
@time df = read_arrow_file(arrow_path)
function read_arrow_file(arrow_path::String)
    println("read arrow file ")
    df = DataFrame(Arrow.Table(arrow_path))
    return df
end

可以正常读出。

在这里插入图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值