Skip to content

Commit

Permalink
ARROW-12677: Changed mask logic to align with other masks. Added memo…
Browse files Browse the repository at this point in the history
…ry_pool since we will invert the mask (requires an allocation) if specified. Added tests for list arrays
  • Loading branch information
westonpace committed May 14, 2021
1 parent 7aa8c7a commit a376647
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
42 changes: 39 additions & 3 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -1593,6 +1593,39 @@ cdef class ListArray(BaseListArray):
Returns
-------
list_array : ListArray
Examples
--------
>>> values = pa.array([1, 2, 3, 4])
>>> offsets = pa.array([0, 2, 4])
>>> pa.ListArray.from_arrays(offsets, values)
<pyarrow.lib.ListArray object at 0x7fbde226bf40>
[
[
0,
1
],
[
2,
3
]
]
# nulls in the offsets array become null lists
>>> offsets = pa.array([0, None, 2, 4])
>>> pa.ListArray.from_arrays(offsets, values)
<pyarrow.lib.ListArray object at 0x7fbde226bf40>
[
[
0,
1
],
null,
[
2,
3
]
]
"""
cdef:
Array _offsets, _values
Expand Down Expand Up @@ -2153,7 +2186,7 @@ cdef class StructArray(Array):
return [pyarrow_wrap_array(arr) for arr in arrays]

@staticmethod
def from_arrays(arrays, names=None, fields=None, mask=None):
def from_arrays(arrays, names=None, fields=None, mask=None, memory_pool=None):
"""
Construct StructArray from collection of arrays representing
each field in the struct.
Expand All @@ -2168,7 +2201,9 @@ cdef class StructArray(Array):
fields : List[Field] (optional)
Field instances for each struct child.
mask : pyarrow.Array[bool] (optional)
Indicate which values are null (False) or not null (True).
Indicate which values are null (True) or not null (False).
memory_pool : MemoryPool (optional)
For memory allocations, if required, otherwise uses default pool.
Returns
-------
Expand Down Expand Up @@ -2199,7 +2234,8 @@ cdef class StructArray(Array):
raise ValueError('Mask must be a pyarray.Array of type bool')
if mask.null_count != 0:
raise ValueError('Mask must not contain nulls')
c_mask = pyarrow_unwrap_buffer(mask.buffers()[1])
inverted_mask = _pc().invert(mask, memory_pool=memory_pool)
c_mask = pyarrow_unwrap_buffer(inverted_mask.buffers()[1])
else:
raise ValueError('Mask must be a pyarray.Array of type bool')

Expand Down
20 changes: 19 additions & 1 deletion python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def test_struct_from_arrays():
arrays = [a, b, c]
fields = [fa, fb, fc]
# With mask
mask = pa.array([False, True, True])
mask = pa.array([True, False, False])
arr = pa.StructArray.from_arrays(arrays, fields=fields, mask=mask)
assert arr.to_pylist() == [None] + expected_list[1:]

Expand Down Expand Up @@ -954,6 +954,24 @@ def test_fixed_size_list_from_arrays():
pa.FixedSizeListArray.from_arrays(values, 5)


def test_variable_list_from_arrays():
values = pa.array([1, 2, 3, 4], pa.int64())
offsets = pa.array([0, 2, 4])
result = pa.ListArray.from_arrays(offsets, values)
assert result.to_pylist() == [[1, 2], [3, 4]]
assert result.type.equals(pa.list_(pa.int64()))

offsets = pa.array([0, None, 2, 4])
result = pa.ListArray.from_arrays(offsets, values)
assert result.to_pylist() == [[1, 2], None, [3, 4]]

# raise if offset out of bounds
with pytest.raises(ValueError):
pa.ListArray.from_arrays(pa.array([-1, 2, 4]), values)

with pytest.raises(ValueError):
pa.ListArray.from_arrays(pa.array([0, 2, 5]), values)

def test_union_from_dense():
binary = pa.array([b'a', b'b', b'c', b'd'], type='binary')
int64 = pa.array([1, 2, 3], type='int64')
Expand Down

0 comments on commit a376647

Please sign in to comment.