pyarrow.register_extension_type#
- pyarrow.register_extension_type(ext_type)#
注册一个 Python 扩展类型。
注册基于扩展名称(因此不同的注册类型需要唯一的扩展名称)。注册时需要一个扩展类型的实例,但注册完成后,该子类的所有实例(无论类型如何参数化)均可使用。
- 参数:
- ext_type
BaseExtensionTypeinstance 要注册的 ExtensionType 子类。
- ext_type
示例
定义一个继承自 ExtensionType 的 RationalType 扩展类型
>>> import pyarrow as pa >>> class RationalType(pa.ExtensionType): ... def __init__(self, data_type: pa.DataType): ... if not pa.types.is_integer(data_type): ... raise TypeError(f"data_type must be an integer type not {data_type}") ... super().__init__( ... pa.struct( ... [ ... ("numer", data_type), ... ("denom", data_type), ... ], ... ), ... # N.B. This name does _not_ reference `data_type` so deserialization ... # will work for _any_ integer `data_type` after registration ... "my_package.rational", ... ) ... def __arrow_ext_serialize__(self) -> bytes: ... # No parameters are necessary ... return b"" ... @classmethod ... def __arrow_ext_deserialize__(cls, storage_type, serialized): ... # return an instance of this subclass ... return RationalType(storage_type[0].type)
注册该扩展类型
>>> pa.register_extension_type(RationalType(pa.int64()))
取消注册该扩展类型
>>> pa.unregister_extension_type("my_package.rational")