Skip to content

Commit

Permalink
ARROW-15210: [Python] Pyarrow compute functions convert args with `__…
Browse files Browse the repository at this point in the history
…arrow_array__`.
  • Loading branch information
coady committed Dec 28, 2021
1 parent 06b1013 commit 9f91d8c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion python/pyarrow/_compute.pyx
Expand Up @@ -416,7 +416,8 @@ cdef class MetaFunction(Function):

cdef _pack_compute_args(object values, vector[CDatum]* out):
for val in values:
if isinstance(val, (list, np.ndarray)):
if isinstance(val, (list, np.ndarray)) or \
hasattr(val, '__arrow_array__'):
val = lib.asarray(val)

if isinstance(val, Array):
Expand Down
9 changes: 9 additions & 0 deletions python/pyarrow/tests/test_compute.py
Expand Up @@ -293,6 +293,15 @@ def test_input_type_conversion():
"foo").to_pylist() == [True, False, None]


def test_input_array_conversion():
class convertible:
def __arrow_array__(self, type=None):
return pa.array(range(5), type)

assert pc.sum(np.arange(5)).as_py() == 10
assert pc.sum(convertible()).as_py() == 10


@pytest.mark.parametrize('arrow_type', numerical_arrow_types)
def test_sum_array(arrow_type):
arr = pa.array([1, 2, 3, 4], type=arrow_type)
Expand Down

0 comments on commit 9f91d8c

Please sign in to comment.