pandas 可以利用 PyArrow 来扩展功能并提高各种 API 的性能。这包括:#

比 NumPy 更广泛的 PyArrow

To use this functionality, please ensure you have installed the minimum supported PyArrow version.

SeriesIndexDataFrame 的列可以直接由 pyarrow.ChunkedArray 支持,这类似于 NumPy 数组。要从主要的 pandas 数据结构中构造这些,您可以将类型字符串后跟 "[pyarrow],例如 "int64[pyarrow]"" 传递给 dtype 参数。#

A Series, Index, or the columns of a DataFrame can be directly backed by a pyarrow.ChunkedArray which is similar to a NumPy array. To construct these from the main pandas data structures, you can pass in a string of the type followed by [pyarrow], e.g. "int64[pyarrow]"" into the dtype parameter

备注

对于接受参数的 PyArrow 类型,您可以将带有这些参数的 PyArrow 类型传递给 ArrowDtype 以用于 dtype 参数。

For PyArrow types that accept parameters, you can pass in a PyArrow type with those parameters into ArrowDtype to use in the dtype parameter.

If you already have an pyarrow.Array or pyarrow.ChunkedArray, you can pass it into arrays.ArrowExtensionArray to construct the associated Series, Index or DataFrame object.

To retrieve a pyarrow pyarrow.ChunkedArray from a Series or Index, you can call the pyarrow array constructor on the Series or Index.

To convert a pyarrow.Table to a DataFrame, you can call the pyarrow.Table.to_pandas() method with types_mapper=pd.ArrowDtype.

操作#

PyArrow 数据结构集成通过 pandas 的 ExtensionArray interface 实现;因此,在 pandas API 中集成此接口的地方就支持该功能。此外,在 PyArrow compute functions 可用时,这些功能还可以通过它们得到加速。这包括:

  • 数值聚合

  • 数值算术

  • 数值四舍五入

  • 逻辑和比较函数

  • 字符串功能

  • 日期时间功能

以下是一些由原生 PyArrow 计算函数加速的操作示例。

I/O 读取#

PyArrow 还提供了 I/O 读取功能,这些功能已被集成到多个 pandas I/O 读取器中。以下函数提供了一个 engine 关键字参数,可以将其分派给 PyArrow 以加速从 I/O 源读取。

默认情况下,这些函数以及所有其他 I/O 读取器函数返回 NumPy 支持的数据。通过指定 dtype_backend="pyarrow" 参数,这些读取器可以返回 PyArrow 支持的数据。读取器不需要设置 engine="pyarrow" 才能必定返回 PyArrow 支持的数据。

一些非 I/O 读取器函数也可以使用 dtype_backend 参数返回 PyArrow 支持的数据,包括: