pyarrow.table#

pyarrow.table(data, names=None, schema=None, metadata=None, nthreads=None)#

从 Python 数据结构或数组序列创建 pyarrow.Table。

参数:
datadict, list, pandas.DataFrame, Arrow 兼容的 table

字符串到数组或 Python 列表的映射、数组或分块数组的列表、pandas DataFrame,或任何实现了 Arrow PyCapsule 协议的表格对象(具有 __arrow_c_array____arrow_c_device_array____arrow_c_stream__ 方法)。

nameslist, 默认 None

当 data 为数组列表时使用的列名。与 ‘schema’ 参数互斥。

schemaSchema, 默认值 None

Arrow Table 的预期 schema。如果未传入,将从数据中推断得出。与 ‘names’ 参数互斥。如果传入,输出将严格遵循此 schema(当 data 为 dict 或 DataFrame 时,若数据中未找到 schema 中的列会报错,且会忽略 schema 中未指定的额外数据)。

metadatadict 或 Mapping, 默认值 None

schema 的可选元数据(如果未传入 schema)。

nthreadsint, 默认值 None

针对 pandas.DataFrame 输入:如果大于 1,则使用指定的线程数并行将列转换为 Arrow。默认情况下,遵循 pyarrow.cpu_count()(最多可使用系统 CPU 总核数)。

返回:

示例

>>> import pyarrow as pa
>>> n_legs = pa.array([2, 4, 5, 100])
>>> animals = pa.array(["Flamingo", "Horse", "Brittle stars", "Centipede"])
>>> names = ["n_legs", "animals"]

从 Python 字典构建 Table

>>> pa.table({"n_legs": n_legs, "animals": animals})
pyarrow.Table
n_legs: int64
animals: string
----
n_legs: [[2,4,5,100]]
animals: [["Flamingo","Horse","Brittle stars","Centipede"]]

从数组构建 Table

>>> pa.table([n_legs, animals], names=names)
pyarrow.Table
n_legs: int64
animals: string
----
n_legs: [[2,4,5,100]]
animals: [["Flamingo","Horse","Brittle stars","Centipede"]]

从带有元数据的数组构建 Table

>>> my_metadata={"n_legs": "Number of legs per animal"}
>>> pa.table([n_legs, animals], names=names, metadata = my_metadata).schema
n_legs: int64
animals: string
-- schema metadata --
n_legs: 'Number of legs per animal'

从 pandas DataFrame 构建 Table

>>> import pandas as pd
>>> df = pd.DataFrame({'year': [2020, 2022, 2019, 2021],
...                    'n_legs': [2, 4, 5, 100],
...                    'animals': ["Flamingo", "Horse", "Brittle stars", "Centipede"]})
>>> pa.table(df)
pyarrow.Table
year: int64
n_legs: int64
animals: large_string
----
year: [[2020,2022,2019,2021]]
n_legs: [[2,4,5,100]]
animals: [["Flamingo","Horse","Brittle stars","Centipede"]]

使用 pyarrow schema 从 pandas DataFrame 构建 Table

>>> my_schema = pa.schema([
...     pa.field('n_legs', pa.int64()),
...     pa.field('animals', pa.string())],
...     metadata={"n_legs": "Number of legs per animal"})
>>> pa.table(df, my_schema).schema
n_legs: int64
animals: string
-- schema metadata --
n_legs: 'Number of legs per animal'
pandas: '{"index_columns": [], "column_indexes": [{"name": null, ...

从分块数组构建 Table

>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> animals = pa.chunked_array([["Flamingo", "Parrot", "Dog"], ["Horse", "Brittle stars", "Centipede"]])
>>> table = pa.table([n_legs, animals], names=names)
>>> table
pyarrow.Table
n_legs: int64
animals: string
----
n_legs: [[2,2,4],[4,5,100]]
animals: [["Flamingo","Parrot","Dog"],["Horse","Brittle stars","Centipede"]]