Skip to content

[Python] Add from_numpy_ndarray and to_numpy_ndarray to ListArray types #35747

Description

@spenczar

Describe the enhancement requested

Interoperation between numpy ndarrays and Arrow's ListArray types (ListArray, LargeListArray, FixedSizeListArray) is a bit tricky.

It's hard to construct values: one must convert to a Python list-of-lists first, which is unnecessarily expensive:

>>>importnumpyasnp>>>importpyarrowaspa>>>np_values=np.ones((3, 2), np.float64())
>>>pa_dtype=pa.list_(pa.float64())
>>>pa_values=pa.array(np_values, type=pa_dtype)
Traceback (mostrecentcalllast):
File"<stdin>", line1, in<module>File"pyarrow/array.pxi", line323, inpyarrow.lib.arrayFile"pyarrow/array.pxi", line83, inpyarrow.lib._ndarray_to_arrayFile"pyarrow/error.pxi", line100, inpyarrow.lib.check_statuspyarrow.lib.ArrowInvalid: onlyhandle1-dimensionalarrays>>>pa_values=pa.array(np_values.tolist(), type=pa_dtype)
<pyarrow.lib.ListArrayobjectat0x11ba433a0>
[
[
1,
1
],
[
1,
1
],
[
1,
1
]
]

Likewise, converting to a numpy ndarray from a Pyarrow ListArray type is tricky, as described in #35622. That issue describes trickiness with FixedSizeListArrays, but the same is true of ListArrays, which often might have equal-length lists in every entry, making them amenable to presentation as an ndarray.

I'd like to propose the following 6 new methods:

  • FixedSizeListArray.from_numpy_ndarray(values, type):
    Constructs a new FixedSizeListArray from values, which must be a numpy ndarray with ndim == 2.
    type is optional; it will be looked up from the ndarray's dtype if unset.
    If type is set, values of the ndarray's dtype must be convertible to the provided type.

  • FixedSizeListArray.to_numpy_ndarray(self):
    Returns the FixedSizeListArray's values as a numpy ndarray with a shape of (len(self), self.type.list_size).

    If any of the FixedSizeListArray's values are null, raises an error.

    If any of the FixedSizeListArray's values contain a null, then returns a ndarray with nan in the null spots, and with dtype set to float64, or None in the null spots and dtype of object if a conversion to float64 is not possible. This matches the behavior of Array.to_numpy for primitive types.

  • ListArray.from_numpy_ndarray(values, type):
    Works just like FixedSizeListArray.from_numpy_ndarray.

  • ListArray.to_numpy_ndarray(self):
    Works like FixedSizeListArray.to_numpy_ndarray, with an additional check that all list elements are of equal length. If any are different, then raises an error.

and same for LargeListArray as for ListArray, bringing the total to 6.

The FixedSizeListArray methods already have an implementation in the FixedShapeTensor extension type. Those implementation are actually a bit more complicated because of tensors' support for permutations:

defto_numpy_ndarray(self):

deffrom_numpy_ndarray(obj):

Component(s)

Python

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions