数据类型和内存中的数据模型#

Apache Arrow 通过将类型元数据与内存缓冲区组合来定义列式数组数据结构,就像在内存和 IO中解释的那样。这些数据结构通过一系列相互关联的类在 Python 中被暴露出来:

  • 类型元数据:pyarrow.DataType 的实例,描述逻辑数组类型。

  • 模式:pyarrow.Schema 的实例,描述一个命名的类型集合。这些可以被视为类似表格对象中的列类型。

  • 数组:pyarrow.Array 的实例,是由 Arrow Buffer 对象组成的原子性、连续的列式数据结构。

  • 记录批次:pyarrow.RecordBatch 的实例,是一组具有特定模式的 Array 对象的集合。

  • 表格:pyarrow.Table 的实例,是一种逻辑表格数据结构,其中每一列由一个或多个相同类型的 pyarrow.Array 对象组成。

类型元数据#

Apache Arrow 定义了与语言无关的列式数组数据结构。这些包括:

  • 固定长度的原始(primitive)类型:数字、布尔值、日期和时间、固定大小的二进制、十进制以及其他可以放入给定数量的值。

  • 可变长度的原始(primitive)类型:二进制、字符串。

  • 嵌套类型:列表、映射、结构体和联合体。

  • 字典类型:一种编码后的分类类型。

Arrow 中的每个逻辑数据类型都有对应的工厂函数,用于在 Python 中创建该类型对象的实例:

import pyarrow as pa

pa.int32()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 import pyarrow as pa
      3 pa.int32()

ModuleNotFoundError: No module named 'pyarrow'
pa.string()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 pa.string()

NameError: name 'pa' is not defined
pa.binary()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[3], line 1
----> 1 pa.binary()

NameError: name 'pa' is not defined
pa.binary(10)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[4], line 1
----> 1 pa.binary(10)

NameError: name 'pa' is not defined
pa.timestamp('ms')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[5], line 1
----> 1 pa.timestamp('ms')

NameError: name 'pa' is not defined

使用“逻辑类型”(logical type)这个名称,是因为一种或多种类型的物理存储(physical storage)可能相同。例如,int64float64timestamp[ms] 每个值都占用 64 位。

这些对象是元数据;它们用于描述数组、模式和记录批次中的数据。在 Python 中,它们可以用在输入数据(例如 Python 对象)可能被强制转换为多个 Arrow 类型的函数中。

Field 类型是类型加上名称和可选的用户定义元数据:

t1 = pa.int32()
f0 = pa.field('int32_field', t1)
f0
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 t1 = pa.int32()
      2 f0 = pa.field('int32_field', t1)
      3 f0

NameError: name 'pa' is not defined
f0.name, f0.type
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[7], line 1
----> 1 f0.name, f0.type

NameError: name 'f0' is not defined

Arrow 支持嵌套值类型,如列表、映射、结构体和联合体。创建这些时,必须传递类型或字段以指示类型的子项的数据类型。例如,我们可以用以下方式定义 int32 值的列表:

t1 = pa.int32()
t6 = pa.list_(t1)
t6
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 t1 = pa.int32()
      2 t6 = pa.list_(t1)
      3 t6

NameError: name 'pa' is not defined

结构体是一组命名字段的集合:

t1 = pa.int32()

t2 = pa.string()

t3 = pa.binary()

t4 = pa.binary(10)

t5 = pa.timestamp('ms')

fields = [
    pa.field('s0', t1),
    pa.field('s1', t2),
    pa.field('s2', t4),
    pa.field('s3', t6),
]


t7 = pa.struct(fields)

print(t7)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[9], line 1
----> 1 t1 = pa.int32()
      3 t2 = pa.string()
      5 t3 = pa.binary()

NameError: name 'pa' is not defined

为了方便,您可以直接传递 (name, type) 元组而不是 Field 实例:

t8 = pa.struct([('s0', t1), ('s1', t2), ('s2', t4), ('s3', t6)])

print(t8)

t8 == t7
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 t8 = pa.struct([('s0', t1), ('s1', t2), ('s2', t4), ('s3', t6)])
      3 print(t8)
      5 t8 == t7

NameError: name 'pa' is not defined

有关数据类型函数的完整列表,请参阅数据类型 API

模式#

Schema 类型与 struct 数组类型类似;它定义了记录批次或表格数据结构中的列名和类型。pyarrow.schema() 工厂函数在 Python 中创建新的 Schema 对象:

my_schema = pa.schema([('field0', t1),
                       ('field1', t2),
                       ('field2', t4),
                       ('field3', t6)])


my_schema
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 my_schema = pa.schema([('field0', t1),
      2                        ('field1', t2),
      3                        ('field2', t4),
      4                        ('field3', t6)])
      7 my_schema

NameError: name 'pa' is not defined

在某些应用中,您可能不需要直接创建模式,只需使用嵌入在 IPC 消息 中的模式。

数组#

对于每种数据类型,都有一个相应的数组数据结构用于保存内存缓冲区,这些缓冲区定义了单个连续的列式数组数据块。当您使用 PyArrow 时,这些数据可能来自 IPC 工具,但也可以从各种类型的 Python 序列(列表、NumPy 数组、pandas 数据)创建。

创建数组的简单方法是使用 pyarrow.array,它类似于 numpy.array 函数。默认情况下,PyArrow 会为您推断数据类型:

arr = pa.array([1, 2, None, 3])

arr
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[12], line 1
----> 1 arr = pa.array([1, 2, None, 3])
      3 arr

NameError: name 'pa' is not defined

但您也可以传递特定的数据类型来覆盖类型推断:

pa.array([1, 2], type=pa.uint16())
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 pa.array([1, 2], type=pa.uint16())

NameError: name 'pa' is not defined

数组的 type 属性是相应的类型元数据部分:

arr.type
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[14], line 1
----> 1 arr.type

NameError: name 'arr' is not defined

每个内存数组都有一个已知的长度和空值计数(如果没有空值,则为 0):

len(arr), arr.null_count
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[15], line 1
----> 1 len(arr), arr.null_count

NameError: name 'arr' is not defined

可以使用常规索引选择标量值。pyarrow.array()None 值转换为 Arrow 空值;返回特殊的 pyarrow.NA 值来表示空值:

arr[0], arr[2]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 arr[0], arr[2]

NameError: name 'arr' is not defined

Arrow 数据是不可变的,因此可以选择值但不能赋值。

数组可以在不复制的情况下进行切片:

arr[1:3]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 arr[1:3]

NameError: name 'arr' is not defined

None 值和 NAN 处理#

如上一节所述,在转换为 pyarrow.Array 时,Python 对象 None 总是被转换为 Arrow 空元素。对于由 Python 对象 float('nan')numpy.nan 表示的浮点数 NaN 值,我们通常在转换过程中将其转换为有效的浮点数值。如果向 pyarrow.array 提供包含 np.nan 的整数输入,将引发 ValueError

为了更好地与 Pandas 兼容,支持将 NaN 值解释为空元素。这在所有 from_pandas 函数上自动启用,并且可以通过将 from_pandas=True 作为函数参数传递给其他转换函数来启用。

列表数组#

pyarrow.array() 能够推断出简单嵌套数据结构(如列表)的类型:

nested_arr = pa.array([[], None, [1, 2], [None, 1]])

print(nested_arr.type)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[18], line 1
----> 1 nested_arr = pa.array([[], None, [1, 2], [None, 1]])
      3 print(nested_arr.type)

NameError: name 'pa' is not defined

结构体数组#

pyarrow.array() 能够从字典数组中推断出结构体类型的模式:

pa.array([{'x': 1, 'y': True}, {'z': 3.4, 'x': 4}])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[19], line 1
----> 1 pa.array([{'x': 1, 'y': True}, {'z': 3.4, 'x': 4}])

NameError: name 'pa' is not defined

结构体数组可以从 Python 字典或元组的序列中初始化。对于元组,您必须显式传递类型:

ty = pa.struct([('x', pa.int8()),
                ('y', pa.bool_())])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[20], line 1
----> 1 ty = pa.struct([('x', pa.int8()),
      2                 ('y', pa.bool_())])

NameError: name 'pa' is not defined
pa.array([{'x': 1, 'y': True}, {'x': 2, 'y': False}], type=ty)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[21], line 1
----> 1 pa.array([{'x': 1, 'y': True}, {'x': 2, 'y': False}], type=ty)

NameError: name 'pa' is not defined
pa.array([(3, True), (4, False)], type=ty)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[22], line 1
----> 1 pa.array([(3, True), (4, False)], type=ty)

NameError: name 'pa' is not defined

初始化结构体数组时,在结构级别和各个字段级别都允许有空值。如果从 Python 字典序列中初始化,缺失的字典键将被视为空值:

pa.array([{'x': 1}, None, {'y': None}], type=ty)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[23], line 1
----> 1 pa.array([{'x': 1}, None, {'y': None}], type=ty)

NameError: name 'pa' is not defined

您还可以从每个结构组件的现有数组构建结构数组。在这种情况下,数据存储将与各个数组共享,不涉及复制:

xs = pa.array([5, 6, 7], type=pa.int16())

ys = pa.array([False, True, True])

arr = pa.StructArray.from_arrays((xs, ys), names=('x', 'y'))
arr.type, arr
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[24], line 1
----> 1 xs = pa.array([5, 6, 7], type=pa.int16())
      3 ys = pa.array([False, True, True])
      5 arr = pa.StructArray.from_arrays((xs, ys), names=('x', 'y'))

NameError: name 'pa' is not defined

映射数组#

可以从元组(键-值对)的列表构建映射数组,但只有在类型显式传递给 pyarrow.array() 函数时才能这样做:

data = [[('x', 1), ('y', 0)], [('a', 2), ('b', 45)]]

ty = pa.map_(pa.string(), pa.int64())

pa.array(data, type=ty)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[25], line 3
      1 data = [[('x', 1), ('y', 0)], [('a', 2), ('b', 45)]]
----> 3 ty = pa.map_(pa.string(), pa.int64())
      5 pa.array(data, type=ty)

NameError: name 'pa' is not defined

映射数组也可以从偏移量、键和项数组构建。偏移量代表每个映射的起始位置。请注意,pyarrow.MapArray.keys()pyarrow.MapArray.items() 属性提供扁平化的键和项。要使键和项与它们的行相关联,请使用 pyarrow.ListArray.from_arrays() 构造函数与 pyarrow.MapArray.offsets 属性。

arr = pa.MapArray.from_arrays([0, 2, 3], ['x', 'y', 'z'], [4, 5, 6])

arr.keys
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[26], line 1
----> 1 arr = pa.MapArray.from_arrays([0, 2, 3], ['x', 'y', 'z'], [4, 5, 6])
      3 arr.keys

NameError: name 'pa' is not defined
arr.items
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[27], line 1
----> 1 arr.items

NameError: name 'arr' is not defined
pa.ListArray.from_arrays(arr.offsets, arr.keys)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[28], line 1
----> 1 pa.ListArray.from_arrays(arr.offsets, arr.keys)

NameError: name 'pa' is not defined
pa.ListArray.from_arrays(arr.offsets, arr.items)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[29], line 1
----> 1 pa.ListArray.from_arrays(arr.offsets, arr.items)

NameError: name 'pa' is not defined

联合数组#

联合类型表示嵌套数组类型,其中每个值可以是(且仅是)一组可能类型中的一个。联合数组有两种可能的存储类型:稀疏和密集。

在稀疏联合数组中,每个子数组的长度与结果联合数组相同。它们与一个 int8 类型的“types”数组相连,该数组指示每个值必须从哪个子数组中选择:

xs = pa.array([5, 6, 7])

ys = pa.array([False, False, True])

types = pa.array([0, 1, 1], type=pa.int8())

union_arr = pa.UnionArray.from_sparse(types, [xs, ys])
union_arr.type, union_arr
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[30], line 1
----> 1 xs = pa.array([5, 6, 7])
      3 ys = pa.array([False, False, True])
      5 types = pa.array([0, 1, 1], type=pa.int8())

NameError: name 'pa' is not defined

在密集联合数组中,除了 int8 类型的“types”数组外,还需要传递 int32 类型的“offsets”数组,该数组指示每个值在选定的子数组中的每个偏移量处可以找到。

xs = pa.array([5, 6, 7])

ys = pa.array([False, True])

types = pa.array([0, 1, 1, 0, 0], type=pa.int8())

offsets = pa.array([0, 0, 1, 1, 2], type=pa.int32())

union_arr = pa.UnionArray.from_dense(types, offsets, [xs, ys])

union_arr.type, union_arr
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[31], line 1
----> 1 xs = pa.array([5, 6, 7])
      3 ys = pa.array([False, True])
      5 types = pa.array([0, 1, 1, 0, 0], type=pa.int8())

NameError: name 'pa' is not defined

字典数组#

PyArrow 中的 Dictionary 类型是一种特殊类型的数组,类似于 R 中的因子或 pandas.Categorical。它允许一个或多个文件或流中的记录批次传输引用共享字典的整数索引,该字典包含逻辑数组中的独特值。这种方法特别常用于字符串以节省内存并提高性能。

在 Apache Arrow 格式中处理字典的方式以及它们在 C++ 和 Python 中的呈现方式略有不同。我们定义了特殊的 DictionaryArray 类型和相应的字典类型。让我们考虑一个例子:

indices = pa.array([0, 1, 0, 1, 2, 0, None, 2])

dictionary = pa.array(['foo', 'bar', 'baz'])

dict_array = pa.DictionaryArray.from_arrays(indices, dictionary)

dict_array
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[32], line 1
----> 1 indices = pa.array([0, 1, 0, 1, 2, 0, None, 2])
      3 dictionary = pa.array(['foo', 'bar', 'baz'])
      5 dict_array = pa.DictionaryArray.from_arrays(indices, dictionary)

NameError: name 'pa' is not defined
dict_array.indices
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[33], line 1
----> 1 dict_array.indices

NameError: name 'dict_array' is not defined
print(dict_array.type)
dict_array.dictionary
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[34], line 1
----> 1 print(dict_array.type)
      2 dict_array.dictionary

NameError: name 'dict_array' is not defined

当在 pandas 中使用 DictionaryArray 时,对应的是 pandas.Categorical

dict_array.to_pandas()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[35], line 1
----> 1 dict_array.to_pandas()

NameError: name 'dict_array' is not defined

记录批次#

在 Apache Arrow 中,记录批次(Record Batch)是一组等长的数组实例。让我们考虑数组集合:

data = [
    pa.array([1, 2, 3, 4]),
    pa.array(['foo', 'bar', 'baz', None]),
    pa.array([True, None, False, True])
]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[36], line 2
      1 data = [
----> 2     pa.array([1, 2, 3, 4]),
      3     pa.array(['foo', 'bar', 'baz', None]),
      4     pa.array([True, None, False, True])
      5 ]

NameError: name 'pa' is not defined

可以使用 RecordBatch.from_arrays 从数组列表创建记录批次。

batch = pa.RecordBatch.from_arrays(data, ['f0', 'f1', 'f2'])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[37], line 1
----> 1 batch = pa.RecordBatch.from_arrays(data, ['f0', 'f1', 'f2'])

NameError: name 'pa' is not defined
batch.num_columns, batch.num_rows
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[38], line 1
----> 1 batch.num_columns, batch.num_rows

NameError: name 'batch' is not defined
batch.schema
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[39], line 1
----> 1 batch.schema

NameError: name 'batch' is not defined
batch[1]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[40], line 1
----> 1 batch[1]

NameError: name 'batch' is not defined

记录批次可以像数组一样进行切片,而无需复制内存。

batch2 = batch.slice(1, 3)

batch2[1]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[41], line 1
----> 1 batch2 = batch.slice(1, 3)
      3 batch2[1]

NameError: name 'batch' is not defined

PyArrow 表格#

PyArrow Table 类型不是 Apache Arrow 规范的一部分,而是一个工具,用于帮助处理多个记录批次和数组片段作为单个逻辑数据集。例如,我们可能需要从套接字流中接收多个小记录批次,然后将它们连接成连续的内存以供 NumPy 或 pandas 使用。Table 对象可以在不要求额外内存复制的情况下实现这一点。

考虑上面创建的记录批次,我们可以使用 Table.from_batches 创建一个包含一个或多个批次副本的表:

batches = [batch] * 5

table = pa.Table.from_batches(batches)

table.num_rows, table
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[42], line 1
----> 1 batches = [batch] * 5
      3 table = pa.Table.from_batches(batches)
      5 table.num_rows, table

NameError: name 'batch' is not defined

表格的列是 ChunkedArray 的实例,它是相同类型的一个或多个数组的容器。

c = table[0]

c
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[43], line 1
----> 1 c = table[0]
      3 c

NameError: name 'table' is not defined
c.num_chunks, c.chunk(0)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[44], line 1
----> 1 c.num_chunks, c.chunk(0)

NameError: name 'c' is not defined

正如您将在 pandas 部分 看到的,我们可以将这些对象转换为连续的 NumPy 数组以供 pandas 使用:

c.to_pandas()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[45], line 1
----> 1 c.to_pandas()

NameError: name 'c' is not defined

如果模式相等,还可以使用 pyarrow.concat_tables() 将多个表连接在一起以形成单个表。

tables = [table] * 2

table_all = pa.concat_tables(tables)

print(table_all.num_rows)

c = table_all[0]

c.num_chunks
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[46], line 1
----> 1 tables = [table] * 2
      3 table_all = pa.concat_tables(tables)
      5 print(table_all.num_rows)

NameError: name 'table' is not defined

这个函数类似于 Table.from_batches,但是它使用表作为输入而不是记录批次。记录批次可以转换为表,但反过来不行,所以如果你的数据已经是表的形式,那么使用 concat_tables()

自定义 Schema 和字段元数据#

Arrow 支持模式级别和字段级别的自定义键值元数据,允许系统插入自己的应用程序定义的元数据以自定义行为。

可以在 pyarrow.Schema.metadata 中访问模式级别的自定义元数据,在 pyarrow.Field.metadata 中访问字段级别的自定义元数据。

请注意,这种元数据在流式处理、序列化和进程间通信(IPC)过程中得以保留。

要自定义现有表的模式元数据,可以使用 pyarrow.Table.replace_schema_metadata() 方法:

table.schema.metadata # empty

table = table.replace_schema_metadata({"f0": "First dose"})

table.schema.metadata
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[47], line 1
----> 1 table.schema.metadata # empty
      3 table = table.replace_schema_metadata({"f0": "First dose"})
      5 table.schema.metadata

NameError: name 'table' is not defined

要自定义表模式中字段的元数据,可以使用 pyarrow.Field.with_metadata() 方法。

field_f1 = table.schema.field("f1")

field_f1.metadata # empty

field_f1 = field_f1.with_metadata({"f1": "Second dose"})

field_f1.metadata
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[48], line 1
----> 1 field_f1 = table.schema.field("f1")
      3 field_f1.metadata # empty
      5 field_f1 = field_f1.with_metadata({"f1": "Second dose"})

NameError: name 'table' is not defined

这两种选项都会创建数据的浅拷贝,并且实际上不会改变不可变的 Schema。要更改表的 Schema 中的元数据,我们在调用 pyarrow.Table.replace_schema_metadata() 时会创建一个新对象。

要更改模式中字段的元数据,我们需要定义一个新模式,并将数据转换为这个新模式:

my_schema2 = pa.schema([
   pa.field('f0', pa.int64(), metadata={"name": "First dose"}),
   pa.field('f1', pa.string(), metadata={"name": "Second dose"}),
   pa.field('f2', pa.bool_())],
   metadata={"f2": "booster"})
t2 = table.cast(my_schema2)

t2.schema.field("f0").metadata
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[49], line 1
----> 1 my_schema2 = pa.schema([
      2    pa.field('f0', pa.int64(), metadata={"name": "First dose"}),
      3    pa.field('f1', pa.string(), metadata={"name": "Second dose"}),
      4    pa.field('f2', pa.bool_())],
      5    metadata={"f2": "booster"})
      6 t2 = table.cast(my_schema2)
      8 t2.schema.field("f0").metadata

NameError: name 'pa' is not defined
t2.schema.field("f1").metadata
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[50], line 1
----> 1 t2.schema.field("f1").metadata

NameError: name 't2' is not defined
t2.schema.metadata
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[51], line 1
----> 1 t2.schema.metadata

NameError: name 't2' is not defined

元数据键值对在 C++ 实现中是 std::string 对象,而在 Python 中则是字节对象(b'...')。

记录批次读取器#

PyArrow 中的许多函数要么返回要么接受 pyarrow.RecordBatchReader 作为参数。它可以像任何记录批次的可迭代对象一样使用,但在获取任何批次之前还提供了它们共同的模式。

schema = pa.schema([('x', pa.int64())])
def iter_record_batches():
   for i in range(2):
      yield pa.RecordBatch.from_arrays([pa.array([1, 2, 3])], schema=schema)
reader = pa.RecordBatchReader.from_batches(schema, iter_record_batches())
print(reader.schema)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[52], line 1
----> 1 schema = pa.schema([('x', pa.int64())])
      2 def iter_record_batches():
      3    for i in range(2):

NameError: name 'pa' is not defined
for batch in reader:
   print(batch)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[53], line 1
----> 1 for batch in reader:
      2    print(batch)

NameError: name 'reader' is not defined

它还可以使用 C 流接口在语言之间发送。