当前位置: 首页 > 科技观察

6600颗星!比Pandas快得多的数据处理库

时间:2023-03-18 15:12:17 科技观察

介绍Polars是一个极快的数据框架库,它使用ApacheArrow列格式作为内存模型,并在Rust中实现。它可以在Rust、Python和Node.js中使用。它具有以下特点:立即执行多线程SIMD查询优化强大的表达式API支持多种语言:Rust、Python等,更多信息请点击此用户指南。Python代码示例>>>df=pl.DataFrame(...{..."A":[1,2,3,4,5],..."fruits":["banana","banana","apple","apple","banana"],..."B":[5,4,3,2,1],..."cars":["beetle","audi","beetle","beetle","beetle"],...}...)#令人尴尬的并行执行#非常有表现力的查询语言>>>(...df....sort("fruits")....select(...[..."fruits",..."cars",...pl.lit("fruits").alias("literal_string_fruits"),...pl.col("B").filter(pl.col("cars")=="beetle").sum(),...pl.col("A").filter(pl.col("B")>2).sum().over("cars").alias("sum_A_by_cars"),#按"cars"分组...pl.col("A").sum().over("fruits").alias("sum_A_by_fruits"),#按"fruits"分组...pl.col("A").reverse().over("fruits").alias("rev_A_by_fruits"),#按“fruits...pl.col("A").sort_by("B").over("fruits").alias("sort_A_by_B_by_fruits"),#groupsby"fruits"...]...)...)shape:(5,8)┌──────────┬──────────┬────────────────┬──────┬────────────────┬──────────────┬──────────────┬────────────┐│fruits┆cars┆literal_stri┆B┆sum_A_by_ca┆sum_A_by_fr┆rev_A_by_fr┆sort_A_by_B││---┆---┆ng_fruits┆---┆rs┆uits┆uits┆_by_fruits││str┆str┆---┆i64┆---┆---┆---┆---││┆┆str┆┆i64┆i64┆i64┆i64│╞══════════╪══════════╪══════════════╪═════╪═════════════╪═════════════╪═════════════╪═════════════╡│“苹果”┆“1”┆“甲虫”1┆4┆7┆4┆4│├??????????┼??????????┼??????????????┼?????┼?????????????┼?????????????┼?????????????┼?????????????┤│“苹果”┆“甲虫”┆“水果”┆11┆4┆7┆3┆3│├??????????┼?????????????????????┕???????????┼?????┼?????????????┼?????????????┼?????????????┼?????????????┤│“香蕉”┆“甲虫”┆“水果”┆11┆4┆8┆5┆5│???????┼??????????┼??????????????┼?????┼?????????????┼?????????????┼?????????????┼?????????????“奥迪”???????┤┆“水果”┆11┆2┆8┆2┆2│├??????????┼??????????┼??????????????????????┼?????┼?????????????┼?????????????┼?????????????┼?????????????┤│“香蕉”┆“甲虫”┆“水果”┆11┆4┆8┆1┆1│└────────────┴──────────┴────────────────┴──────┴────────────┴──────────────┴────────────┴────────────┘性能量Polars速度非常快,事实上,它是目前性最好的解决方案之一具体可参看h2oai的db基准测试结果。此时我们自己用一些示例代码来比python的中pandas和polars推理。importpandasaspdimportpolarsasplimporttimeit#读取时间比start_df=timeit.default_timer()df=pd.read_csv("/Users/lenskit/Desktop/aa.csv")df=df.sort_values("company_name",ascending=False).head()stop_df=timeit.default_timer()print('time:',stop_df-start_df)start_pl=timeit.default_timer()data=pl.read_csv("/Users/lenskit/Desktop/aa.csv")data.sort(by="company_name",reverse=True).head()stop_pl=timeit.default_timer()print('time1:',stop_pl-start_pl)#纵向拼接时间对比start_df1=timeit.default_timer()df_1=pd.read_csv('/Users/lenskit/Desktop/aa.csv')df_2=pd.read_csv('/Users/lenskit/Desktop/bb.csv')df_1.append(df_2,ignore_index=True)stop_df1=timeit.default_timer()print('time2:',stop_df1-start_df1)start_pl1=timeit.default_timer()pl_1=pl.read_csv('/Users/lenskit/Desktop/aa.csv')pl_2=pl.read_csv('/Users/lenskit/Desktop/bb.csv')pl_1.vstack(pl_2)stop_pl1=timeit.default_timer()print('time3:',stop_pl1-start_pl1)time:5.088931238time1:0.8967700230000002time2:4.707102063time3:0.639797883可以看出polars在读取文件方面比pandas快5倍以上,在垂直数据拼接方面polars比pandas快7倍以上。Python安装使用以下语句安装最新的polars版本:$pip3install-Upolars[pyarrow]Polars目前更新频繁(每周/每隔几天),因此最好定期更新polars以获得最新的错误修复/特征。Rust安装你可以从http://crates.io获取最新版本,或者如果你想使用最新的特性/性能改进,你可以使用以下命令指向该版本的master分支。polars={git="https://github.com/pola-rs/polars",rev=""}注意需要Rust版本>=1.58文档想知道Polars支持的所有特性?阅读文档!Python安装指南:$pip3installpolarsPython文档https://link.zhihu.com/?target=https%3A//pola-rs.github.io/polars/py-polars/html/reference/index.html用户指南https://link.zhihu.com/?target=https%3A//pola-rs.github.io/polars-book/user-guide/index.htmlRustRust文档(主分支)https://link.zhihu。com/?target=https%3A//pola-rs.github.io/polars/polars/index.html用户指南https://link.zhihu.com/?target=https%3A//pola-rs.github.io/polars-book/user-guide/index.htmlNode安装指南:yarninstallnodejs-polarsNode文档https://link.zhihu.com/?target=https%3A//pola-rs.github.io/polars/nodejs-polars/html/index.html用户指南https://link.zhihu.com/?target=https%3A//pola-rs.github.io/polars-book/user-guide/index.html[Python]:从源代码编译polars如果你想要最新版本或最佳性能,你应该从源代码编译Polar。这可以通过按顺序执行以下步骤来完成:1.安装最新的Rust编译器2.安装maturin:$pip3installmaturin3.选择以下选项之一:最快的二进制文件,编译时间很长:$cdpy-polars&&maturindevelop--rustc-extra-args="-Ctarget-cpu=native"--release更快的二进制文件,更短的编译时间:$cdpy-polars&&maturindevelop--rustc-extra-args="-Ccodegen-units=16-Clto=需要注意的是,Python实现的Rustcrate叫做py-polars,以区别于Rustcrate包polars本身。但是,Python包和Python模块都被命名为polars,所以你可以pipinstallpolars并导入polars。Arrow2Polars已移至arrow2。Arrow2是ApacheArrowColumnarFormat的更快、更安全的实现。Arrow2还具有更细粒度的代码库,有助于减少编译器膨胀。