This is an automated email from the ASF dual-hosted git repository.

jorisvandenbossche pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 7e44aeb62a GH-30058: [Python] Add StructType attribute to access all 
its fields (#43481)
7e44aeb62a is described below

commit 7e44aeb62a5660f60ee57a635072041fbb407cba
Author: Abhinand-J <[email protected]>
AuthorDate: Fri Aug 9 10:59:03 2024 -0700

    GH-30058: [Python] Add StructType attribute to access all its fields 
(#43481)
    
    
    
    ### Rationale for this change
    Currently you cannot directly access the names of all the fields within a 
StructType, and it is not obvious how to access each field either. This change 
allows people to directly access the fields and their names directly. See 
#30058.
    
    ### What changes are included in this PR?
    I added a .names and .fields attribute to StructType.
    .names returns a list of the names of the fields in the StructType, and 
.fields returns a list of the fields in the StructType.
    
    ### Are these changes tested?
    Yes. I made two tests to check if the new .names and .fields attributes 
would operate as intended. The tests check if the .names attribute would return 
a list of the names of each field and if .fields returns a list of the fields.
    
    ### Are there any user-facing changes?
    Yes, there are two new attributes users can now use.
    
    * GitHub Issue: #30058
    
    Authored-by: Abhinand-J <[email protected]>
    Signed-off-by: Joris Van den Bossche <[email protected]>
---
 python/pyarrow/tests/test_types.py |  2 ++
 python/pyarrow/types.pxi           | 27 +++++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/python/pyarrow/tests/test_types.py 
b/python/pyarrow/tests/test_types.py
index 2dce1e613d..d673f95652 100644
--- a/python/pyarrow/tests/test_types.py
+++ b/python/pyarrow/tests/test_types.py
@@ -693,6 +693,8 @@ def test_struct_type():
     assert list(ty) == fields
     assert ty[0].name == 'a'
     assert ty[2].type == pa.int32()
+    assert ty.names == [f.name for f in ty]
+    assert ty.fields == list(ty)
     with pytest.raises(IndexError):
         assert ty[3]
 
diff --git a/python/pyarrow/types.pxi b/python/pyarrow/types.pxi
index ab20d74dd9..039870accd 100644
--- a/python/pyarrow/types.pxi
+++ b/python/pyarrow/types.pxi
@@ -1025,6 +1025,33 @@ cdef class StructType(DataType):
     def __reduce__(self):
         return struct, (list(self),)
 
+    @property
+    def names(self):
+        """
+        Lists the field names.
+
+        Examples
+        --------
+        >>> import pyarrow as pa
+        >>> struct_type = pa.struct([('a', pa.int64()), ('b', pa.float64()), 
('c', pa.string())])
+        >>> struct_type.names
+        ['a', 'b', 'c']
+        """
+        return [f.name for f in self]
+
+    @property
+    def fields(self):
+        """
+        Lists all fields within the StructType.
+
+        Examples
+        --------
+        >>> import pyarrow as pa
+        >>> struct_type = pa.struct([('a', pa.int64()), ('b', pa.float64()), 
('c', pa.string())])
+        >>> struct_type.fields
+        [pyarrow.Field<a: int64>, pyarrow.Field<b: double>, pyarrow.Field<c: 
string>]
+        """
+        return list(self)
 
 cdef class UnionType(DataType):
     """

Reply via email to