加入收藏 | 设为首页 | 会员中心 | 我要投稿 珠海站长网 (https://www.0756zz.cn/)- 云服务器、边缘计算、基础存储、云计算、中间件!
当前位置: 首页 > 云计算 > 正文

6.6K Star!比 Pandas 快很多的数据处理库

发布时间:2022-08-02 12:16:13 所属栏目:云计算 来源:互联网
导读:Polars 是一个使用 Apache Arrow 列格式作为内存模型,用Rust实现的,在Rust, Python以及Node.js中均可使用的速度极快的数据帧库。 它有以下几个特点: 懒|立即执行 多线程 SIMD 查询优化 强大的表达式API 支持多种语言:Rust、Python 等 了解更多内容可以点
  Polars 是一个使用 Apache Arrow 列格式作为内存模型,用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"],
  ...     }
  ... )
 
  # embarrassingly parallel execution
  # very expressive query language
  >>> (
  ...     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"),     # groups by "cars"
  ...             pl.col("A").sum().over("fruits").alias("sum_A_by_fruits"),                         # groups by "fruits"
  ...             pl.col("A").reverse().over("fruits").alias("rev_A_by_fruits"),                     # groups by "fruits
  ...             pl.col("A").sort_by("B").over("fruits").alias("sort_A_by_B_by_fruits"),            # groups by "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         │
  ╞══════════╪══════════╪══════════════╪═════╪═════════════╪═════════════╪═════════════╪═════════════╡
  │ "apple"  ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 7           ┆ 4           ┆ 4           │
  ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
  │ "apple"  ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 7           ┆ 3           ┆ 3           │
  ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
  │ "banana" ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 8           ┆ 5           ┆ 5           │
  ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
  │ "banana" ┆ "audi"   ┆ "fruits"     ┆ 11  ┆ 2           ┆ 8           ┆ 2           ┆ 2           │
  ├╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌╌╌┤
  │ "banana" ┆ "beetle" ┆ "fruits"     ┆ 11  ┆ 4           ┆ 8           ┆ 1           ┆ 1           │
  └──────────┴──────────┴──────────────┴─────┴─────────────┴─────────────┴─────────────┴─────────────┘
  1.
  2.
  3.
  4.
  5.
  6.
  7.
  8.
  9.
  10.
  11.
  12.
  13.
  14.
  15.
  16.
  17.
  18.
  19.
  20.
  21.
  22.
  23.
  24.
  25.
  26.
  27.
  28.
  29.
  30.
  31.
  32.
  33.
  34.
  35.
  36.
  37.
  38.
  39.
  40.
  41.
  42.
  43.
  44.
  性能
  Polars速度非常快,事实上,它是目前性能最好的解决方案之一。具体可参见 h2oai's db基准测试结果 。
 
  此处我们自己用一些示例代码来对比python中pandas和polars处理数据的速度差距。
 
  复制
  import pandas as pd
  import polars as pl
  import timeit
 
  # 读取时间对比
  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.088931238
  time1:  0.8967700230000002
  time2:  4.707102063
  time3:  0.639797883
  1.
  2.
  3.
  4.
  5.
  6.
  7.
  8.
  9.
  10.
  11.
  12.
  13.
  14.
  15.
  16.
  17.
  18.
  19.
  20.
  21.
  22.
  23.
  24.
  25.
  26.
  27.
  28.
  29.
  30.
  31.
  32.
  33.
  34.
  35.
  36.
  37.
  可以看到在读取文件上,polars比pandas速度快了5倍多,在数据纵向拼接上,polars比pandas快了有7倍多。
 
  Python安装
  用如下语句安装最新的polars版本:
 
  复制
  $ pip3 install -U polars[pyarrow]
  1.
  目前polars的更新频率很高(每周/每隔几天),所以最好定期更新一下polars来获得最新的错误修复/功能。
 
  Rust安装
  您可以从 http:// crates.io 获取最新版本,或者如果你想使用最新的功能/性能改进,可以用如下命令指向版本的master分支。
 
  复制
  polars = { git = "https://github.com/pola-rs/polars", rev = "<optional git tag>" }
  1.
  注意需要Rust version >=1.58
 
  文档
  想知道Polars支持的所有功能吗?阅读文档!
 
  Python
 
  安装指南:$ pip3 install polars
  Python文档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.html
  Rust
 
  Rust文件(主分支)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.html
  Node
 
  安装指南:yarn install nodejs-polars
  Node文档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​ :$ pip3 install maturin
  3、选择以下任一:
  最快的二进制文件,非常长的编译时间:
  $ cd py-polars && maturin develop --rustc-extra-args="-C target-cpu=native" --release
  较快的二进制文件,短一些的编译时间:
  $ cd py-polars && maturin develop --rustc-extra-args="-C codegen-units=16 -C lto=
  需要注意的是,Python实现的Rust crate被称为 py-polars ,以区别于Rust crate包 polars 本身。然而,Python包和Python模块都被命名为 polars ,所以你可以 pip install polars 和 import polars 。
 
  Arrow2
  Polars已经转移到 arrow2 。Arrow2是 Apache Arrow Columnar Format 更快、更安全的实现。Arrow2还具有更细粒度的代码库,有助于减少编译器膨胀。

(编辑:珠海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读