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

alenka 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 e588ed175f GH-42014: [Python] Let StructArray.from_array accept a type 
in addition to names or fields (#43047)
e588ed175f is described below

commit e588ed175f9058010d61c90c8b804b3f9bcf3be0
Author: shinespiked <[email protected]>
AuthorDate: Wed Jul 31 09:35:30 2024 -0400

    GH-42014: [Python] Let StructArray.from_array accept a type in addition to 
names or fields (#43047)
    
    ### Rationale for this change
    
    StructArray.from_array currently accepts names or fields to create the 
struct array. However if you already have a struct type it's more convenient to 
pass that in and allow the function to use it to build the StructArray instead 
of the user having to pull out the fields themselves.
    
    ### What changes are included in this PR?
    
    Add a new argument to StructArray.from_array called structtype. The 
function will prevent both fields and structype from being passed by raising a 
ValueError. If structtype is not null then the existing fields argument is set 
from the structtype fields. This allows all of the existing code in the 
function to remain untouched.
    
    ### Are these changes tested?
    
    Yes. Testing creating the structarray from fields a test is added to make 
sure that a struct type can be used to create the array.
    
    ### Are there any user-facing changes?
    Yes, the StructArray.from_arrays function now has an extra optional argument
    * GitHub Issue: #42014
    
    Authored-by: Akshay Subramanian 
<[email protected]>
    Signed-off-by: AlenkaF <[email protected]>
---
 python/pyarrow/array.pxi           | 14 ++++++++++++--
 python/pyarrow/tests/test_array.py |  7 +++++++
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index b1f90cd165..997f208a5d 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -3977,12 +3977,12 @@ cdef class StructArray(Array):
 
     @staticmethod
     def from_arrays(arrays, names=None, fields=None, mask=None,
-                    memory_pool=None):
+                    memory_pool=None, type=None):
         """
         Construct StructArray from collection of arrays representing
         each field in the struct.
 
-        Either field names or field instances must be passed.
+        Either field names, field instances or a struct type must be passed.
 
         Parameters
         ----------
@@ -3995,6 +3995,8 @@ cdef class StructArray(Array):
             Indicate which values are null (True) or not null (False).
         memory_pool : MemoryPool (optional)
             For memory allocations, if required, otherwise uses default pool.
+        type : pyarrow.StructType (optional)
+            Struct type for name and type of each child. 
 
         Returns
         -------
@@ -4013,6 +4015,14 @@ cdef class StructArray(Array):
             Field py_field
             DataType struct_type
 
+        if fields is not None and type is not None:
+            raise ValueError('Must pass either fields or type, not both')
+
+        if type is not None:
+            fields = []
+            for field in type:
+                fields.append(field)
+
         if names is None and fields is None:
             raise ValueError('Must pass either names or fields')
         if names is not None and fields is not None:
diff --git a/python/pyarrow/tests/test_array.py 
b/python/pyarrow/tests/test_array.py
index 30d258b9aa..c44ec3f8e1 100644
--- a/python/pyarrow/tests/test_array.py
+++ b/python/pyarrow/tests/test_array.py
@@ -707,6 +707,13 @@ def test_struct_from_arrays():
     assert not arr.type[0].nullable
     assert arr.to_pylist() == expected_list
 
+    # From structtype
+    structtype = pa.struct([fa, fb, fc])
+    arr = pa.StructArray.from_arrays([a, b, c], type=structtype)
+    assert arr.type == pa.struct([fa, fb, fc])
+    assert not arr.type[0].nullable
+    assert arr.to_pylist() == expected_list
+
     with pytest.raises(ValueError):
         pa.StructArray.from_arrays([a, b, c], fields=[fa, fb])
 

Reply via email to