pyarrow.register_extension_type — Apache Arrow v22.0.0 (original) (raw)
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_typeso deserialization ... # will work for any integerdata_typeafter 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")