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/20 10:00:07 UTC

[GitHub] [arrow] jorisvandenbossche commented on a diff in pull request #13652: ARROW-17131: [Python] add StructType().field(): returns a field by name or index

jorisvandenbossche commented on code in PR #13652:
URL: https://github.com/apache/arrow/pull/13652#discussion_r925414671


##########
python/pyarrow/types.pxi:
##########
@@ -494,6 +505,36 @@ cdef class StructType(DataType):
         """
         return self.struct_type.GetFieldIndex(tobytes(name))
 
+    def field(self, i):
+        """
+        Select a field by its column name or numeric index.
+
+        Parameters
+        ----------
+        i : int or str
+
+        Returns
+        -------
+        pyarrow.Field
+
+        Examples
+        --------
+
+        >>> import pyarrow as pa
+        >>> struct_type = pa.struct({'x': pa.int32(), 'y': pa.string()})
+
+        Select the second field:
+
+        >>> struct_type.field(1)
+        pyarrow.Field<y: string>
+
+        Select the field of the column named 'x':
+
+        >>> struct_type.field('x')
+        pyarrow.Field<x: int32>
+        """
+        return self.__getitem__(i)

Review Comment:
   I would maybe do it the other way around: move the current code of getitem here, and in getitem call `return self.field(key)` (that feels a bit more typical pattern than explicitly calling a dunder method here)



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