技术 | Polars vs Pandas 速度测试:一个 Rust 重写的魔法
date
Feb 17, 2023
slug
substitute-pandas-with-polars-a-dataframe-module-rewritten-in-rust
status
Published
summary
Polars在速度测试中表现优于Pandas,常用操作时间仅为Pandas的四分之一或更少,尤其在导入CSV、拼接数据框和进行统计操作时,显示出显著的性能优势。
tags
Engineering
Data Analysis
Python
type
Post
auth
Polars 是一个我早有耳闻但从未实际使用的 Pandas 替代品。根据 Polars 自己的介绍,它是一个“超级快的数据框”(blazingly fast DataFrames)——王婆卖瓜能信吗?本文在自己的常用环境测试了一下,确实很快。
测试结果
柱状图可见 Polars 常用操作所用时间均为 Pandas 的 1/4 甚至更少:

详细表格:
Task | Pandas | Polars |
Import a 10mb csv file | 0.157s | 0.055s |
Column loops | 0.168s | 0.060s |
Concat three 10mb dataframes | 0.063s | 0.016s |
Groupby() and sum() | 0.008s | 0.002s |
测试方法
环境
- Apple Silicon M1 (2020, the cheapest one)
- MacOS 13
- Jupyter Notebook in VSCode
- Python ==3.10.9
- pandas==1.5.3
- polars==0.16.6
任务
- 导入一个 10MB 的 csv 文件,并指定 spe & encoding,这是很日常的任务
- 拼接重复 df 为 1 个
- Groupby 和 sum 的简单统计操作
- 根据每个列名,循环进行 groupby 和 sum 的循环统计操作
参考
- Polars 开源仓库:https://github.com/pola-rs/polars