本文介绍如何使用PyPolars库加快Pandas工作流程。

 使用PyPolars,让Pandas快三倍 PyPolars 数据 开源 第1张

【51CTO.com快译】Pandas是数据科学家处理数据的最重要的Python软件包之一。Pandas库主要用于数据探索和可视化,它随带大量的内置函数。Pandas无法处理大型数据集,因为它无法在CPU的所有核心上扩展或分布进程。

为了加快计算速度,您可以使用CPU的所有核心,并加快工作流程。有各种开源库,包括Dask、Vaex、Modin、Pandarallel和PyPolars等,它们可以在CPU的多个核心上并行处理计算。我们在本文中将讨论PyPolars库的实现和用法,并将其性能与Pandas库进行比较。

PyPolars是什么?

PyPolars是一个类似Pandas的开源Python数据框库。PyPolars利用CPU的所有可用核心,因此处理计算比Pandas更快。PyPolars有一个类似Pandas的API。它是用Rust和Python包装器编写的。

理想情况下,当数据对于Pandas而言太大、对于Spark而言太小时,使用 PyPolars。

PyPolars如何工作?

PyPolars库有两个API,一个是Eager API,另一个是Lazy API。Eager API与Pandas的API非常相似,执行完成后立即获得结果,这类似Pandas。Lazy API与Spark非常相似,一执行查询,就形成地图或方案。然后在CPU的所有核心上并行执行。

 使用PyPolars,让Pandas快三倍 PyPolars 数据 开源 第2张

图1. PyPolars API

PyPolars基本上是连接到Polars库的Python绑定。PyPolars库好用的地方是,其API与Pandas相似,这使开发人员更容易使用。

安装:

可以使用以下命令从PyPl安装 PyPolars:

  1. pipinstallpy-polars

并使用以下命令导入库:

  1. iportpypolarsaspl

基准时间约束:

为了演示,我使用了一个含有2500万个实例的大型数据集(~6.4Gb)。

 使用PyPolars,让Pandas快三倍 PyPolars 数据 开源 第3张

图2. Pandas和Py-Polars基本操作的基准时间数

针对使用Pandas和PyPolars库的一些基本操作的上述基准时间数,我们可以观察到 PyPolars几乎比Pandas快2到3倍。

现在我们知道PyPolars有一个与Pandas非常相似的API,但仍没有涵盖Pandas的所有函数。比如说,PyPolars中就没有.describe()函数,相反我们可以使用df_PyPolars.to_pandas().describe()。

用法:

  1. importpandasaspd
  2. importnumpyasnp
  3. importpypolarsaspl
  4. importtime
  5. WARNING!
  6. py-polarswasrenamedtopolars,pleaseinstallpolars!
  7. https://pypi.org/project/polars/
  8. path="data.csv"

读取数据:

  1. s=time.time()
  2. df_pandas=pd.read_csv(path)
  3. e=time.time()
  4. pd_time=e-s
  5. print("PandasLoadingTime={}".format(pd_time))
  6. C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3071:DtypeWarning:Columns(2,7,14)havemixedtypes.Specifydtypeoptiononimportorsetlow_memory=False.
  7. has_raised=awaitself.run_ast_nodes(code_ast.body,cell_name,
  8. PandasLoadingTime=217.1734380722046
  9. s=time.time()
  10. df_pypolars=pl.read_csv(path)
  11. e=time.time()
  12. pl_time=e-s
  13. print("PyPolarsLoadingTime={}".format(pl_time))
  14. PyPolarsLoadingTime=114.0408570766449

shape:

  1. s=time.time()
  2. print(df_pandas.shape)
  3. e=time.time()
  4. pd_time=e-s
  5. print("PandasShapeTime={}".format(pd_time))
  6. (25366521,19)
  7. PandasShapeTime=0.0
  8. s=time.time()
  9. print(df_pypolars.shape)
  10. e=time.time()
  11. pl_time=e-s
  12. print("PyPolarsShapeTime={}".format(pl_time))
  13. (25366521,19)
  14. PyPolarsShapeTime=0.0010192394256591797

过滤:

  1. s=time.time()
  2. temp=df_pandas[df_pandas['PAID_AMT']>500]
  3. e=time.time()
  4. pd_time=e-s
  5. print("PandasFilterTime={}".format(pd_time))
  6. PandasFilterTime=0.8010377883911133
  7. s=time.time()
  8. temp=df_pypolars[df_pypolars['PAID_AMT']>500]
  9. e=time.time()
  10. pl_time=e-s
  11. print("PyPolarsFilterTime={}".format(pl_time))
  12. PyPolarsFilterTime=0.7790462970733643

Groupby:

  1. s=time.time()
  2. temp=df_pandas.groupby(by="MARKET_SEGMENT").agg({'PAID_AMT':np.sum,'QTY_DISPENSED':np.mean})
  3. e=time.time()
  4. pd_time=e-s
  5. print("PandasGroupByTime={}".format(pd_time))
  6. PandasGroupByTime=3.5932095050811768
  7. s=time.time()
  8. temp=df_pypolars.groupby(by="MARKET_SEGMENT").agg({'PAID_AMT':np.sum,'QTY_DISPENSED':np.mean})
  9. e=time.time()
  10. pd_time=e-s
  11. print("PyPolarsGroupByTime={}".format(pd_time))
  12. PyPolarsGroupByTime=1.2332513110957213

运用函数:

  1. %%time
  2. s=time.time()
  3. temp=df_pandas['PAID_AMT'].apply(round)
  4. e=time.time()
  5. pd_time=e-s
  6. print("PandasLoadingTime={}".format(pd_time))
  7. PandasLoadingTime=13.081078290939331
  8. Walltime:13.1s
  9. s=time.time()
  10. temp=df_pypolars['PAID_AMT'].apply(round)
  11. e=time.time()
  12. pd_time=e-s
  13. print("PyPolarsLoadingTime={}".format(pd_time))
  14. PyPolarsLoadingTime=6.03610580444336

值计算:

  1. %%time
  2. s=time.time()
  3. temp=df_pandas['MARKET_SEGMENT'].value_counts()
  4. e=time.time()
  5. pd_time=e-s
  6. print("PandasValueCountsTime={}".format(pd_time))
  7. PandasValueCountsTime=2.8194501399993896
  8. Walltime:2.82s
  9. %%time
  10. s=time.time()
  11. temp=df_pypolars['MARKET_SEGMENT'].value_counts()
  12. e=time.time()
  13. pd_time=e-s
  14. print("PyPolarsValueCountsTime={}".format(pd_time))
  15. PyPolarsValueCountsTime=1.7622406482696533
  16. Walltime:1.76s

描述:

  1. %%time
  2. s=time.time()
  3. temp=df_pandas.describe()
  4. e=time.time()
  5. pd_time=e-s
  6. print("PandasDescribeTime={}".format(pd_time))
  7. PandasDescribeTime=15.48347520828247
  8. Walltime:15.5s
  9. %%time
  10. s=time.time()
  11. temp=df_pypolars[temp_cols].to_pandas().describe()
  12. e=time.time()
  13. pd_time=e-s
  14. print("PyPolarsDescribeTime={}".format(pd_time))
  15. PyPolarsDescribeTime=44.31892013549805
  16. Walltime:44.3s

去重:

  1. iportpypolarsaspl
0

保存数据:

  1. iportpypolarsaspl
1

结论

我们在本文中简要介绍了PyPolars库,包括它的实现、用法以及在一些基本操作中将其基准时间数与Pandas相比较的结果。请注意,PyPolars的工作方式与Pandas非常相似, PyPolars是一种节省内存的库,因为它支持的内存是不可变内存。

可以阅读说明文档详细了解该库。还有其他各种开源库来并行处理Pandas操作,并加快进程。

参考资料:

Polars说明文档和GitHub存储库:https://github.com/ritchie46/polars

[1] Polars Documentation and GitHub repository: https://github.com/ritchie46/polars

原文标题:Make Pandas 3 Times Faster with PyPolars,作者:Satyam Kumar

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

转载请说明出处
知优网 » 使用PyPolars,让Pandas快三倍

发表评论

您需要后才能发表评论