Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1317,29 +1317,31 @@ cdef class StructArray(Array):
ssize_t num_arrays
ssize_t length
ssize_t i
DataType struct_type

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")

length = len(arrays[0])

c_arrays.resize(num_arrays)
for i in range(num_arrays):
array = arrays[i]
if len(array) != length:
raise ValueError("All arrays must have the same length")
c_arrays[i] = array.sp_array

cdef DataType struct_type = struct([
field(name, array.type)
for name, array in
zip(names, arrays)
if num_arrays != len(names):
raise ValueError("The number of names should be equal to the "
"number of arrays")

if num_arrays > 0:
length = len(arrays[0])
c_arrays.resize(num_arrays)
for i in range(num_arrays):
array = arrays[i]
if len(array) != length:
raise ValueError("All arrays must have the same length")
c_arrays[i] = array.sp_array
else:
length = 0

struct_type = struct([
field(name, array.type) for name, array in zip(names, arrays)
])

c_result.reset(new CStructArray(struct_type.sp_type, length, c_arrays))
Expand Down
4 changes: 2 additions & 2 deletions python/pyarrow/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ def _ensure_filesystem(fs):
# interface and return it
if not issubclass(fs_type, FileSystem):
for mro in inspect.getmro(fs_type):
if mro.__name__ is 'S3FileSystem':
if mro.__name__ == 'S3FileSystem':
return S3FSWrapper(fs)
# In case its a simple LocalFileSystem (e.g. dask) use native arrow
# FS
elif mro.__name__ is 'LocalFileSystem':
elif mro.__name__ == 'LocalFileSystem':
return LocalFileSystem.get_instance()

raise IOError('Unrecognized filesystem: {0}'.format(fs_type))
Expand Down
9 changes: 9 additions & 0 deletions python/pyarrow/tests/test_convert_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,11 @@ def test_empty_range():


def test_structarray():
arr = pa.StructArray.from_arrays([], names=[])
assert arr.type == pa.struct([])
assert len(arr) == 0
assert arr.to_pylist() == []

ints = pa.array([None, 2, 3], type=pa.int64())
strs = pa.array([u'a', None, u'c'], type=pa.string())
bools = pa.array([True, False, None], type=pa.bool_())
Expand All @@ -883,6 +888,10 @@ def test_structarray():
pylist = arr.to_pylist()
assert pylist == expected, (pylist, expected)

# len(names) != len(arrays)
with pytest.raises(ValueError):
pa.StructArray.from_arrays([ints], ['ints', 'strs'])


def test_struct_from_dicts():
ty = pa.struct([pa.field('a', pa.int32()),
Expand Down