You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/07/06 14:55:58 UTC

[GitHub] [arrow] wjones127 commented on a diff in pull request #13454: ARROW-13612: [Python] Allow specifying a custom type for converting ExtensionScalar to python object

wjones127 commented on code in PR #13454:
URL: https://github.com/apache/arrow/pull/13454#discussion_r914942510


##########
docs/source/python/extending_types.rst:
##########
@@ -286,6 +286,33 @@ 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 custom
+3D point class 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: ListScalar) -> Point3D:
+            return Point3D(*scalar.as_py())
+
+Arrays built using this extension type now provide scalars that convert to our ``Point3D`` class::
+
+    >>> storage = pa.array([[1, 2, 3], [4, 5, 6]], pa.list_(pa.float32(), 3))
+    >>> arr = pa.ExtensionArray.from_storage(Point3DType(), storage)
+    >>> arr[0].as_py()
+    Point3D(x=1.0, y=2.0, z=3.0)
+

Review Comment:
   Maybe it might be compelling to also show that `as_py_list()` returns `List[Point3D]` too.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org