Skip to content

Commit

Permalink
apacheGH-39515: [PYTHON] Pass in type to MapType.from_arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Fokko committed Jan 8, 2024
1 parent 1362122 commit 805ff9e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
19 changes: 13 additions & 6 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -2467,7 +2467,7 @@ cdef class MapArray(ListArray):
"""

@staticmethod
def from_arrays(offsets, keys, items, MemoryPool pool=None):
def from_arrays(offsets, keys, items, DataType type=None, MemoryPool pool=None):
"""
Construct MapArray from arrays of int32 offsets and key, item arrays.
Expand Down Expand Up @@ -2564,11 +2564,18 @@ cdef class MapArray(ListArray):
_keys = asarray(keys)
_items = asarray(items)

with nogil:
out = GetResultValue(
CMapArray.FromArrays(_offsets.sp_array,
_keys.sp_array,
_items.sp_array, cpool))
if type is not None:
with nogil:
out = GetResultValue(
CMapArray.FromArraysAndType(
type.sp_type, _offsets.sp_array,
_keys.sp_array, _items.sp_array))
else:
with nogil:
out = GetResultValue(
CMapArray.FromArrays(_offsets.sp_array,
_keys.sp_array,
_items.sp_array, cpool))
cdef Array result = pyarrow_wrap_array(out)
result.validate()
return result
Expand Down
8 changes: 8 additions & 0 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,14 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
const shared_ptr[CArray]& items,
CMemoryPool* pool)

@staticmethod
CResult[shared_ptr[CArray]] FromArraysAndType" FromArrays"(
shared_ptr[CDataType],
const shared_ptr[CArray]& offsets,
const shared_ptr[CArray]& keys,
const shared_ptr[CArray]& items,
CMemoryPool* pool)

shared_ptr[CArray] keys()
shared_ptr[CArray] items()
CMapType* map_type()
Expand Down
6 changes: 6 additions & 0 deletions python/pyarrow/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,12 @@ def test_map_from_arrays():

assert result.equals(expected)

result = pa.MapArray.from_arrays(offsets, keys, items, pa.map_(
keys.type,
items.type
))
assert result.equals(expected)

# check invalid usage

offsets = [0, 1, 3, 5]
Expand Down

0 comments on commit 805ff9e

Please sign in to comment.