pyarrow.register_extension_type#

pyarrow.register_extension_type(ext_type)#

Register a Python extension type.

Registration is based on the extension name (so different registered types need unique extension names). Registration needs an extension type instance, but then works for any instance of the same subclass regardless of parametrization of the type.

Parameters:
ext_typeBaseExtensionType instance

The ExtensionType subclass to register.

Examples

Define a RationalType extension type subclassing ExtensionType:

>>> 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)

Register the extension type:

>>> pa.register_extension_type(RationalType(pa.int64()))

Unregister the extension type:

>>> pa.unregister_extension_type("my_package.rational")