This is an automated email from the ASF dual-hosted git repository.
wesm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/master by this push:
new 2e9f7dd ARROW-1706: [Python] Coerce array inputs to
StructArray.from_arrays. Flip order of arguments
2e9f7dd is described below
commit 2e9f7dda2bf207d75a638749233b064fa049292a
Author: Wes McKinney <[email protected]>
AuthorDate: Fri Feb 2 12:27:22 2018 -0500
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 <[email protected]>
Closes #1512 from wesm/ARROW-1706 and squashes the following commits:
786c37d0 [Wes McKinney] Raise error when names is None in
StructArray.from_arrays
990dda57 [Wes McKinney] Fix API change
2053d941 [Wes McKinney] Add test case
9c229498 [Wes McKinney] Flip order of arguments to StructArray.from_arrays,
try to coerce non-pyarrow data to Array
---
python/pyarrow/array.pxi | 24 ++++++++++++++++++++++--
python/pyarrow/tests/test_convert_builtin.py | 28 ++++++++++++++++++++++++++--
python/pyarrow/tests/test_convert_pandas.py | 4 ++--
3 files changed, 50 insertions(+), 6 deletions(-)
diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi
index caeefd2..9b6ae0f 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 bbdf6e7..ce54f23 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 f1f40a6..3109907 100644
--- a/python/pyarrow/tests/test_convert_pandas.py
+++ b/python/pyarrow/tests/test_convert_pandas.py
@@ -1154,8 +1154,8 @@ class TestPandasConversion(object):
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},
--
To stop receiving notification emails like this one, please contact
[email protected].