最近看到Arrow格式,感觉设计很牛B,具体就不介绍了。所以实操了解一下。说明一下,python中有pyarrow库,而不是Arrow库(时间日期库)。这个库的功能很强大,可以处理多种数据格式。

一、材料准备
准备了一个csv文件,大约约59万行,14列,大小约61M,格式如下:

table shape row:  589680
table shape col:  14

为了测试需要,在最后一列,open_interest这一列补上0,减少缺省处理。
有了这个csv材料,便可以转成Dataframe,进而转成parquet格式,或feather格式。

二、具体代码

import numpy as np
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
import time as t# 生成arrow格式
print("write parquet file....")
csv_path = "C:\\Users\\songroom\\Desktop\\test.csv"
time_0 = t.time()
df = pd.read_csv(csv_path)
time_1 =t.time()
print("read csv cost :", time_1-time_0)
print("type of df : ",type(df))
time_2 = t.time()
table = pa.Table.from_pandas(df)
time_3 = t.time()
print("type of table :",type(table))
print("Dataframe convert table:", time_3-time_2)
#
print("write to parquet to disk.....")
time_4 = t.time()
pq_path = "C:\\Users\\songroom\\Desktop\\test.parquet"
pq.write_table(table, pq_path)
time_5 = t.time()
print("write  parquet cost :", time_5-time_4)print("read parquet file from disk ....")
table2 = pq.read_table(pq_path)time_6 = t.time()
print("read  parquet cost :", time_6-time_5)
print("type of table2 :",type(table2))
print("table shape row: ",table2.shape[0])
print("table shape col: ",table2.shape[1])

三、文件大小比较

1、读写性能、文件大小
生成parquet文件,大约是11.3M,和原来的csv文件比,大约是20%,这个很省空间呀。

读写速度具体比较如下:

write parquet file....
read csv cost : 1.0619995594024658
type of df :  <class 'pandas.core.frame.DataFrame'>
type of table : <class 'pyarrow.lib.Table'>
Dataframe convert table: 0.08900094032287598
write to parquet to disk.....
write  parquet cost : 0.3249986171722412
read parquet file from disk ....
read  parquet cost : 0.05600690841674805
type of table2 : <class 'pyarrow.lib.Table'>
table2 shape row:  589680
table2 shape col:  14

也就是说,对一个59万行的文件测试,结果是:
csv pk parquet(1.06s PK 0.056s )用时比:5%
csv pk parquet (61M PK 11.3M ) 空间占比:20%
总体上,总结一下,parquet性能良好,多快好省。

当然这个数量级和不同运行环境并不一定相同,谨供参考。

说明的是,parquet文件通过压缩软件再压缩的空间不太大,11.3M->7M水平。

2、parquet与字符串
因为样本csv文件的第一列是时间字符串,“2010-01-01 9:30:00”这种格式,如果把这个换成数值型,这个速度会略提高一些。

(base) D:\py_joinquant>python -u "d:\py_joinquant\test.py"
write parquet file....
read csv cost : 0.6745004653930664
type of df :  <class 'pandas.core.frame.DataFrame'>
type of table : <class 'pyarrow.lib.Table'>
Dataframe convert table: 0.010273456573486328
write to parquet to disk.....
write  parquet cost : 0.28311967849731445
read parquet file from disk ....
read  parquet cost : 0.04045557975769043
type of table2 : <class 'pyarrow.lib.Table'>
table shape row:  589680
table shape col:  14

大体上是,0.056 s pk 0.040s 。总体上对字符串没有特别大的压力。

四、和Feather比较

还是同一个csv文件,我们用feather处理一下,比较一下读的速度。feather读出来是DataFrame格式,而parquet读出来是Table格式。

1、和Julia的Feather下测试

using DataFrames
using CSV
using Feathercsv_path = s"C:\Users\songroom\Desktop\test.csv"
println("csv => DataFrame: ")
df = @time CSV.File(csv_path) |> DataFrame;
ft_path = s"C:\Users\songroom\Desktop\ft.ft"
println("DataFrame=> Feather:")
@time ft_file = Feather.write(ft_path,df)
println("read Feather")
@time ft = Feather.read(ft_path)
println("type of ft :",typeof(ft))

在同一台电脑julia程序测试如下:
可见,同样的文件,feather格式要比parquet格式快。feather还有一个优点是对字符串不太挑,相比parquet,字符串敏感性更低。补充一下,hdf5对字符串是最敏感的。

0.022 s pk 0.056 s

速度上,julia feather要快于 python parquet 。但并不清楚,Julia本身比Python在语言本身上的速度影响,只能初步估计feather在读上应比parquet。

需要说明的是,feather文件的可压缩空间很大,压缩后只占原来文件的5-8%左右。

2、julia下读python生成的parquet文件

julia有Parquet.jl库,但还是有点问题,主要是性能特别慢。

(1)性能问题

# test.jl
using Parquet
filename = s"C:\Users\songroom\Desktop\test.parquet";
@time pq = ParFile(filename) ;
println("type of pq : ",typeof(pq));
println("c_names: ", colnames(pq));
println("rows : ",nrows(pq));
println("cols : ",ncols(pq));
julia> @run test0.001709 seconds (3.13 k allocations: 197.570 KiB)
type of pq : ParFile
c_names: [["dt"], ["open"], ["close"], ["low"], ["high"], ["volume"], ["money"], ["factor"], ["high_limit"], ["low_limit"], ["avg"], ["pre_close"], ["paused"], ["open_interest"]]
rows : 589680
cols : 14

可以看到,这个速度有多快,要比python快太多了!

但是这个ParFile格式目前还不是很友好,不象dataframe一样。所以速度并不能简单相比。

利用ParquetFiles库,转换成dataframe输出:

using ParquetFiles
filename = s"C:\Users\songroom\Desktop\test.parquet";
println("ParquetFiles : parquet => df")
@time df = load(filename) |> DataFrame

结果:

ParquetFiles : parquet => df21.306288 seconds (210.99 M allocations: 8.567 GiB, 8.11% gc time)

可以看出,ParquetFiles和Parquet目前还存在严重的性能问题尚未解决,期盼!github的issues区大家也提出相同的性能问题。【—做个记号2020-11-22,性能问题!】

(2)跨语言交互:通畅
目前用julia库读python写的parquet文件,目前是可以的,上面的试验已经表明。也就是说parquet的文件有跨语言通用性,这个和csv文件类似。

3、Python下feather测试
在python 找到feather库。代码如下:

import feather
print("now is feather....");
ft_path = "C:\\Users\\songroom\\Desktop\\test.feather"
time_7 = t.time()
feather.write_dataframe(df, ft_path)
time_8 = t.time()
print("feather write_dataframe : " ,time_8-time_7)
time_9 = t.time()
df2 = feather.read_dataframe(ft_path)
time_10 = t.time()
print("feather read_dataframe : ",time_10-time_9)

输出结果:

now is feather....
feather write_dataframe :  0.06961703300476074
feather read_dataframe :  0.1556386947631836

或者上面的代码换成下面的,也是一样的:

print("pyarrow.feather.....")
import pyarrow.feather as feather
time_11 = t.time()
feather.write_feather(df, ft_path)
time_12 = t.time()
print("write feather cost :",time_12-time_11)
time_13 = t.time()
feather.read_feather(ft_path);
time_14 = t.time()
print("read feather cost :",time_14-time_13)

结果:
1、python 的feather读速度远低于parquet格式,这个不知什么原因。
2、python的feather读速度远低于julia feather格式,这个是0.16s pk 0.025 s; 这个差距也太大了! 这个也不知什么原因。

结论:

如果你要快,可以考虑julia feather; 如果又要快,但不吃硬盘,那就用parquet,建议目前阶段用python,生态比较成熟。julia parquet目前还需成熟。

总之,parquet是一种值得关注的,具备不同语言可读通用性的数据格式。

Python与Julia : parquet、feather格式比较相关推荐

  1. 在Python中文件用Feather格式,与 CSV说再见,速度提升 150 倍!

    转载:再见 CSV,速度提升 150 倍!_菜鸟学Python的博客-CSDN博客 为什么要和CSV再见? 好了说了那么久,来介绍下为什么要和CSV再见.其实也谈不上彻底再见吧,日常还是要用的,这里再 ...

  2. 通达信日线数据转换为feather格式,提高后续数据处理速度

    沪深两市股票数量目前为6798只,选股所需要的数据处理时间较长.通过对比,发现使用feather格式可以明显减少数据读写时间,因此,通过修改前面的程序,将数据保存为feather格式. 需要安装fea ...

  3. 通达信日线数据用转换为excel、csv和feather格式

    python功能虽然强大,但由于术业有专攻,不少股民朋友不擅长写代码,对python望而却步.今天开始写一个小软件,主要作用是可以直接运行python代码,进行股票数据处理和分析. 上一篇文章介绍了股 ...

  4. 在R、Python和Julia中常用的数据可视化技术

    俗话说"一图胜千言".通过各种图片和图形化展示,我们可以更清晰地表达很多抽象概念.理论.数据模式或某些想法.在本章中,我们首先解释为什么应该关心数据可视化.然后,我们将讨论几种在R ...

  5. python使用fpdf生成发票格式的pdf文件包含:文字、图片logo、表格、条形码等;

    python使用fpdf生成发票格式的pdf文件包含:文字.图片logo.表格.条形码等: pip install fpdf #python使用fpdf生成发票格式的pdf文件包含:文字.图片logo ...

  6. python使用configparser读取ini格式的配置文件

    python使用configparser读取ini格式的配置文件 来自Python标准库的configparser模块定义了读取和写入Microsoft Windows操作系统使用的配置文件的功能.这 ...

  7. cpythonjava解释xml_详解python使用lxml操作xml格式文件

    python利用lxml读写xml格式的文件 之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便. 1. 写xml文件 a) 用etree和objectify fr ...

  8. python循环语句-Python for循环控制语句一般格式及方法

    for循环语句是python中的一个循环控制语句,任何有序的序列对象内的元素都可以遍历,比如字符串.列表List.元组等可迭代对像.之前讲过的if语句虽然和for语句用法不同,但可以用在for语句下做 ...

  9. python中的数字类型格式与运算_Python中的数字类型格式与运算

    出品 Python中的数字(Digital)与数学中的数字相同,可以通过各种各样的运算符实现各种计算,在Python中数字类型的数据格式分为以下几种: 1.整数型(integer) 2.浮点数(flo ...

  10. python typing typescript_将 python 数据转化为 TypeScript 格式

    前段时间写过这篇文章将 python 数据转化为 lua 格式, 这段时间因为新项目改用 Creator + TypeScript 的原因, 需要导出 ts 格式的数据. 当然我们可以选择使用 jso ...

最新文章

  1. 完毕port(CompletionPort)具体解释 - 手把手教你玩转网络编程系列之三
  2. ExecutorService框架
  3. mysql树形遍历_mysql树形结构遍历
  4. Java后台管理系统,开箱即用
  5. c语言传入的指针无返回值,c语言 关于指针注意事项
  6. 蔬菜名称大全500种_市场上常见47种室外健身器材型号及名称大全
  7. 测试开发:聊一聊自动化测试框架,值得收藏!
  8. 9.运输层(3)---TCP
  9. 软件构架 课堂练习一
  10. 机器学习算法(八):基于BP神经网络的预测(乳腺癌分类实践)
  11. Hibernate 二级缓存的作用
  12. dev-cpp linux,QT4+Devcpp开发环境搭建(参照网上资料整理)
  13. Git遇到的问题:This is not a valid source path/URL
  14. 百度竞价推广之关键词选择技巧
  15. 每天学习10句英语-第七天
  16. 《如何阅读一本书》读书笔记
  17. 2020-10-17(学生管理系统)
  18. 不使用redis,在前后端分离项目的条件下将验证码进行储存
  19. ggplot2读书笔记9:第六章 标度(二)
  20. python爬虫爬取新闻实战01:小白如何迅速学会爬虫爬取上千条新闻

热门文章

  1. Ubuntu修改open files数
  2. BZOj 4540: [Hnoi2016]序列 [莫队 st表 预处理]
  3. C库函数学习笔记之strcpy
  4. spring-第十八篇之spring AOP基于XML配置文件的管理方式
  5. 缓存与缓冲的区别 cache与buffer的区别
  6. Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array(动态规划.递推)
  7. The Introduction Of Filter
  8. 【洛谷P1507 NASA的食物计划】动态规划
  9. [BZOJ4300]绝世好题
  10. 130.C++经典面试题 52-100