[ 
https://issues.apache.org/jira/browse/ARROW-1706?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16350728#comment-16350728
 ] 

ASF GitHub Bot commented on ARROW-1706:
---------------------------------------

wesm closed pull request #1512: ARROW-1706: [Python] Coerce array inputs to 
StructArray.from_arrays. Flip order of arguments
URL: https://github.com/apache/arrow/pull/1512
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index caeefd2ff..9b6ae0fc6 100644
--- a/python/pyarrow/array.pxi
+++ b/python/pyarrow/array.pxi
@@ -828,8 +828,23 @@ cdef class DictionaryArray(Array):
 
 
 cdef class StructArray(Array):
+
     @staticmethod
-    def from_arrays(field_names, arrays):
+    def from_arrays(arrays, names=None):
+        """
+        Construct StructArray from collection of arrays representing each field
+        in the struct
+
+        Parameters
+        ----------
+        arrays : sequence of Array
+        names : List[str]
+            Field names
+
+        Returns
+        -------
+        result : StructArray
+        """
         cdef:
             Array array
             shared_ptr[CArray] c_array
@@ -839,6 +854,11 @@ cdef class StructArray(Array):
             ssize_t length
             ssize_t i
 
+        if names is None:
+            raise ValueError('Names are currently required')
+
+        arrays = [asarray(x) for x in arrays]
+
         num_arrays = len(arrays)
         if num_arrays == 0:
             raise ValueError("arrays list is empty")
@@ -855,7 +875,7 @@ cdef class StructArray(Array):
         cdef DataType struct_type = struct([
             field(name, array.type)
             for name, array in
-            zip(field_names, arrays)
+            zip(names, arrays)
         ])
 
         c_result.reset(new CStructArray(struct_type.sp_type, length, c_arrays))
diff --git a/python/pyarrow/tests/test_convert_builtin.py 
b/python/pyarrow/tests/test_convert_builtin.py
index bbdf6e71e..ce54f23eb 100644
--- a/python/pyarrow/tests/test_convert_builtin.py
+++ b/python/pyarrow/tests/test_convert_builtin.py
@@ -493,8 +493,8 @@ def test_structarray():
     strs = pa.array([u'a', None, u'c'], type=pa.string())
     bools = pa.array([True, False, None], type=pa.bool_())
     arr = pa.StructArray.from_arrays(
-        ['ints', 'strs', 'bools'],
-        [ints, strs, bools])
+        [ints, strs, bools],
+        ['ints', 'strs', 'bools'])
 
     expected = [
         {'ints': None, 'strs': u'a', 'bools': True},
@@ -529,3 +529,27 @@ def test_struct_from_dicts():
                 {'a': None, 'b': None, 'c': None},
                 {'a': None, 'b': 'bar', 'c': None}]
     assert arr.to_pylist() == expected
+
+
+def test_structarray_from_arrays_coerce():
+    # ARROW-1706
+    ints = [None, 2, 3]
+    strs = [u'a', None, u'c']
+    bools = [True, False, None]
+    ints_nonnull = [1, 2, 3]
+
+    arrays = [ints, strs, bools, ints_nonnull]
+    result = pa.StructArray.from_arrays(arrays,
+                                        ['ints', 'strs', 'bools',
+                                         'int_nonnull'])
+    expected = pa.StructArray.from_arrays(
+        [pa.array(ints, type='int64'),
+         pa.array(strs, type='utf8'),
+         pa.array(bools),
+         pa.array(ints_nonnull, type='int64')],
+        ['ints', 'strs', 'bools', 'int_nonnull'])
+
+    with pytest.raises(ValueError):
+        pa.StructArray.from_arrays(arrays)
+
+    assert result.equals(expected)
diff --git a/python/pyarrow/tests/test_convert_pandas.py 
b/python/pyarrow/tests/test_convert_pandas.py
index ca2f1e361..7cc6e7ac2 100644
--- a/python/pyarrow/tests/test_convert_pandas.py
+++ b/python/pyarrow/tests/test_convert_pandas.py
@@ -1153,8 +1153,8 @@ def test_structarray(self):
         strs = pa.array([u'a', None, u'c'], type=pa.string())
         bools = pa.array([True, False, None], type=pa.bool_())
         arr = pa.StructArray.from_arrays(
-            ['ints', 'strs', 'bools'],
-            [ints, strs, bools])
+            [ints, strs, bools],
+            ['ints', 'strs', 'bools'])
 
         expected = pd.Series([
             {'ints': None, 'strs': u'a', 'bools': True},


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> [Python] StructArray.from_arrays should handle sequences that are coercible 
> to arrays
> -------------------------------------------------------------------------------------
>
>                 Key: ARROW-1706
>                 URL: https://issues.apache.org/jira/browse/ARROW-1706
>             Project: Apache Arrow
>          Issue Type: Improvement
>          Components: Python
>            Reporter: Wes McKinney
>            Assignee: Wes McKinney
>            Priority: Major
>              Labels: pull-request-available
>             Fix For: 0.9.0
>
>
> Currently the arrays passed must be `pyarrow.Array` objects already.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to