wjones127 commented on code in PR #13454:
URL: https://github.com/apache/arrow/pull/13454#discussion_r908769271
##########
docs/source/python/extending_types.rst:
##########
@@ -286,6 +286,36 @@ are available).
The same ``__arrow_ext_class__`` specialization can be used with custom types
defined
by subclassing :class:`ExtensionType`.
+Custom scalar conversion
+~~~~~~~~~~~~~~~~~~~~~~~~
+
+If you want scalars of your custom extension type to convert to a custom type
when
+:meth:`ExtensionScalar.as_py()` is called, you can override the
:meth:`ExtensionType.scalar_as_py()`
+method. For example, if we wanted the above example 3D point type to return a
tuple instead
+of a list, we would implement::
+
+ Point3D = namedtuple("Point3D", ["x", "y", "z"])
+
+ class Point3DType(pa.PyExtensionType):
+ def __init__(self):
+ pa.PyExtensionType.__init__(self, pa.list_(pa.float32(), 3))
+
+ def __reduce__(self):
+ return Point3DType, ()
+
+ def scalar_as_py(self, scalar):
+ if scalar is None:
+ return None
+ else:
+ return Point3D(*scalar.as_py())
+
+Arrays built using this extension scalar type now have the expected scalar
behaviour::
Review Comment:
```suggestion
Arrays built using this extension type now provide scalars that convert to
our ``Point3D`` class::
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]