数据存储格式选择:Parquet、HDF还是Feather?

2,014次阅读
没有评论

## 前言

最近在写的策略框架需要对数据进行频繁读取,传统的sqlite肯定不考虑,因为数据量比较大,读取速度太慢,所以考虑使用其他格式存储数据。常见的df存储格式有csv,hdf5,feather,Parquet,本文将对这几种格式进行测试,看看哪一种格式最适合我。

## 测试代码

参考了以下代码:https://github.com/stefan-jansen/machine-learning-for-trading/blob/master/02_market_and_fundamental_data/05_storage_benchmark/storage_benchmark.ipynb

“`python
from pathlib import Path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import random
import string

results = {}

def generate_test_data(nrows=1000, numerical_cols=200, text_cols=0, text_length=10):
s = “”.join([random.choice(string.ascii_letters)
for _ in range(text_length)])
data = pd.concat([pd.DataFrame(np.random.random(size=(nrows, numerical_cols))),
pd.DataFrame(np.full(shape=(nrows, text_cols), fill_value=s))],
axis=1, ignore_index=True)
data.columns = [str(i) for i in data.columns]
return data

data_type = ‘Numeric’

df = generate_test_data(numerical_cols=200, text_cols=10) # 数据生成
df.info()

parquet_file = Path(‘test.parquet’) # 以parquet为例
df.to_parquet(parquet_file)
size = parquet_file.stat().st_size

%%timeit -o
df = pd.read_parquet(parquet_file) # 读取
read = _
parquet_file.unlink()

%%timeit -o
df.to_parquet(parquet_file) # 写入
parquet_file.unlink()
write = _

results[‘Parquet’] = {‘read’: np.mean(read.all_runs), ‘write’: np.mean(write.all_runs), ‘size’: size}
results[‘Parquet’] # 测试结果保存
“`

pandas读取写入这些格式的代码:

“`python
df = pd.read_parquet(parquet_file) # parquet读取
df.to_parquet(parquet_file) # parquet写入

with pd.HDFStore(test_store) as store:
store.get(key) # HDF Fixed、HDF Table、HDF Select读取

with pd.HDFStore(test_store) as store:
store.put(key, df) # HDF Fixed写入
store.append(‘file’, df, format=’t’) # HDF Table写入
store.append(‘file’, df, format=’t’, data_columns=[‘company’, ‘form’]) # HDF Select写入

pd.read_csv(test_csv) # csv读取
df.to_csv(test_csv) # csv写入

pd.read_feather(test_feather) # feather读取
df.to_feather(test_feather) # feather写入
“`

## 测试结果

共测试了三个不同大小的数据集,其中表头为数据集占用内存大小,因为电脑性能原因,我没办法测试更大的数据了(实际上HDF Table和Select就因为内存爆了所以没有测试结果)。测试结果如下。

| | | 1.6+MB | 152.6+ MB | 1.5+ GB |
| – | – | – | – | – |
| **Read** | Parquet | 22.5 ms | 697 ms | 5.14 s |
| | HDF Fixed | 9.9 ms | 3.21 s | 81 s |
| | HDF Table | 16.1 ms | 7.41 s | |
| | HDF Select | 15.6 ms | 7.36 s | |
| | csv | 47.5 ms | 3.92 s | 41.9 s |
| | feather | 3.79 ms | 390 ms | 4.14 s |
| **Write** | Parquet | 34.4 ms | 3.71 s | 33.7 s |
| | HDF Fixed | 11.9 ms | 3.23 s | 51 s |
| | HDF Table | 17.9 ms | 8.49 s | |
| | HDF Select | 17.8 ms | 8.49 s | |
| | csv | 301 ms | 18.3 s | 191 s |
| | feather | 13 ms | 1.98 s | 17.2 s |
| **Size** | Parquet | 1.88MB | 94.12MB | 966.96MB |
| | HDF Fixed | 2.67MB | 201.41MB | 2004.86MB |
| | HDF Table | 1.76MB | 172MB | |
| | HDF Select | 1.76MB | 172MB | |
| | csv | 3.79MB | 288.74MB | 2887.42MB |
| | feather | 1.67MB | 209.95MB | 2098.23MB |

可以看到,三种不同大小的数据集中,feather读写速度都一枝独秀,大小占用中规中矩。Parquet在小数据集上表现较差,但随着数据量的增加,其读写速度相比与其他格式就有了很大优势,在大数据集上,Parquet的读取速度甚至能和feather一较高下,可以想象数据量突破2G后,Parquet的读取速度可能就是最快的了。但是在写方面,Parquet一直没有表现出超越feather的势头。Parquet另外一个优势就是压缩率高,占用空间相比于其他格式一直是比较小的。hdf比较尴尬,在小数据集上打不过feather,在大数据集上的读取速度甚至比不上csv。至于csv就不用说了,各方面拉跨。

值得注意的是,feather虽然快,但是它不支持dataframe的index,也就是说你传入带index的df它会报错……

看了测试结果,相信你心中已经有了各个格式的应用场景。尴尬的是,我的单个数据集刚好只有40M,这个场景下用Parquet感觉没啥必要……Read More 

正文完
可以使用微信扫码关注公众号(ID:xzluomor)
post-qrcode
 0
评论(没有评论)

文心AIGC

2023 年 3 月
 12345
6789101112
13141516171819
20212223242526
2728293031  
文心AIGC
文心AIGC
人工智能ChatGPT,AIGC指利用人工智能技术来生成内容,其中包括文字、语音、代码、图像、视频、机器人动作等等。被认为是继PGC、UGC之后的新型内容创作方式。AIGC作为元宇宙的新方向,近几年迭代速度呈现指数级爆发,谷歌、Meta、百度等平台型巨头持续布局
文章搜索
热门文章
潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026

潞晨尤洋:日常办公没必要上私有模型,这三类企业才需要 | MEET2026 Jay 2025-12-22 09...
面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25

面向「空天具身智能」,北航团队提出星座规划新基准丨NeurIPS’25 鹭羽 2025-12-13 22:37...
商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1

商汤Seko2.0重磅发布,合作短剧登顶抖音AI短剧榜No.1 十三 2025-12-15 14:13:14 ...
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
最新评论
ufabet ufabet มีเกมให้เลือกเล่นมากมาย: เกมเดิมพันหลากหลาย ครบทุกค่ายดัง
tornado crypto mixer tornado crypto mixer Discover the power of privacy with TornadoCash! Learn how this decentralized mixer ensures your transactions remain confidential.
ดูบอลสด ดูบอลสด Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Pretty! This has been a really wonderful post. Many thanks for providing these details.
ดูบอลสด ดูบอลสด Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
Obrazy Sztuka Nowoczesna Obrazy Sztuka Nowoczesna Thank you for this wonderful contribution to the topic. Your ability to explain complex ideas simply is admirable.
ufabet ufabet Hi there to all, for the reason that I am genuinely keen of reading this website’s post to be updated on a regular basis. It carries pleasant stuff.
ufabet ufabet You’re so awesome! I don’t believe I have read a single thing like that before. So great to find someone with some original thoughts on this topic. Really.. thank you for starting this up. This website is something that is needed on the internet, someone with a little originality!
ufabet ufabet Very well presented. Every quote was awesome and thanks for sharing the content. Keep sharing and keep motivating others.
热评文章
跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026

跳过“逐字生成”!蚂蚁集团赵俊博:扩散模型让我们能直接修改Token | MEET2026 一水 2025-1...
10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了

10亿美元OpenAI股权兑换迪士尼版权!米老鼠救Sora来了 一水 2025-12-12 13:56:19 ...
IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别

IDC MarketScape: 容联云位居“中国AI赋能的联络中心”领导者类别 量子位的朋友们 2025-1...