Skip to content

Commit 2e9f7dd

Browse files
committed
ARROW-1706: [Python] Coerce array inputs to StructArray.from_arrays. Flip order of arguments
I flipped the argument order to be more consistent with the same methods in RecordBatch, Table. The StructArray method doesn't seem to be widely used so I'm not sure there's the need to go through a deprecation cycle Author: Wes McKinney <wes.mckinney@twosigma.com> Closes #1512 from wesm/ARROW-1706 and squashes the following commits: 786c37d [Wes McKinney] Raise error when names is None in StructArray.from_arrays 990dda5 [Wes McKinney] Fix API change 2053d94 [Wes McKinney] Add test case 9c22949 [Wes McKinney] Flip order of arguments to StructArray.from_arrays, try to coerce non-pyarrow data to Array
1 parent 5042863 commit 2e9f7dd

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

python/pyarrow/array.pxi

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,8 +828,23 @@ cdef class DictionaryArray(Array):
828828

829829

830830
cdef class StructArray(Array):
831+
831832
@staticmethod
832-
def from_arrays(field_names, arrays):
833+
def from_arrays(arrays, names=None):
834+
"""
835+
Construct StructArray from collection of arrays representing each field
836+
in the struct
837+
838+
Parameters
839+
----------
840+
arrays : sequence of Array
841+
names : List[str]
842+
Field names
843+
844+
Returns
845+
-------
846+
result : StructArray
847+
"""
833848
cdef:
834849
Array array
835850
shared_ptr[CArray] c_array
@@ -839,6 +854,11 @@ cdef class StructArray(Array):
839854
ssize_t length
840855
ssize_t i
841856

857+
if names is None:
858+
raise ValueError('Names are currently required')
859+
860+
arrays = [asarray(x) for x in arrays]
861+
842862
num_arrays = len(arrays)
843863
if num_arrays == 0:
844864
raise ValueError("arrays list is empty")
@@ -855,7 +875,7 @@ cdef class StructArray(Array):
855875
cdef DataType struct_type = struct([
856876
field(name, array.type)
857877
for name, array in
858-
zip(field_names, arrays)
878+
zip(names, arrays)
859879
])
860880

861881
c_result.reset(new CStructArray(struct_type.sp_type, length, c_arrays))

python/pyarrow/tests/test_convert_builtin.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ def test_structarray():
493493
strs = pa.array([u'a', None, u'c'], type=pa.string())
494494
bools = pa.array([True, False, None], type=pa.bool_())
495495
arr = pa.StructArray.from_arrays(
496-
['ints', 'strs', 'bools'],
497-
[ints, strs, bools])
496+
[ints, strs, bools],
497+
['ints', 'strs', 'bools'])
498498

499499
expected = [
500500
{'ints': None, 'strs': u'a', 'bools': True},
@@ -529,3 +529,27 @@ def test_struct_from_dicts():
529529
{'a': None, 'b': None, 'c': None},
530530
{'a': None, 'b': 'bar', 'c': None}]
531531
assert arr.to_pylist() == expected
532+
533+
534+
def test_structarray_from_arrays_coerce():
535+
# ARROW-1706
536+
ints = [None, 2, 3]
537+
strs = [u'a', None, u'c']
538+
bools = [True, False, None]
539+
ints_nonnull = [1, 2, 3]
540+
541+
arrays = [ints, strs, bools, ints_nonnull]
542+
result = pa.StructArray.from_arrays(arrays,
543+
['ints', 'strs', 'bools',
544+
'int_nonnull'])
545+
expected = pa.StructArray.from_arrays(
546+
[pa.array(ints, type='int64'),
547+
pa.array(strs, type='utf8'),
548+
pa.array(bools),
549+
pa.array(ints_nonnull, type='int64')],
550+
['ints', 'strs', 'bools', 'int_nonnull'])
551+
552+
with pytest.raises(ValueError):
553+
pa.StructArray.from_arrays(arrays)
554+
555+
assert result.equals(expected)

python/pyarrow/tests/test_convert_pandas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,8 @@ def test_structarray(self):
11541154
strs = pa.array([u'a', None, u'c'], type=pa.string())
11551155
bools = pa.array([True, False, None], type=pa.bool_())
11561156
arr = pa.StructArray.from_arrays(
1157-
['ints', 'strs', 'bools'],
1158-
[ints, strs, bools])
1157+
[ints, strs, bools],
1158+
['ints', 'strs', 'bools'])
11591159

11601160
expected = pd.Series([
11611161
{'ints': None, 'strs': u'a', 'bools': True},

0 commit comments

Comments
 (0)