Skip to content

[Python] Support converting list of multi-dimensional arrays to FixedShapeTensor #49644

Closed

Description

@jorisvandenbossche

Describe the enhancement requested

When you want to construct a fixed shape tensor array, not from a single contiguous ndarray, but from a list of the individual array elements, this "accidentally" works for 1d arrays (because we support that for ListArray):

>>> pa.array([np.array([1,2,3]), np.array([2,3,4])], type=pa.fixed_shape_tensor(pa.int64(), shape=(3,)))
<pyarrow.lib.FixedShapeTensorArray object at 0x7fadc4d1f3a0>
[
  [
    1,
    2,
    3
  ],
  [
    2,
    3,
    4
  ]
]

But trying the same for >1 d arrays, you get the following error:

>>> pa.array([np.array([[1,2,3]]), np.array([[2,3,4]])], type=pa.fixed_shape_tensor(pa.int64(), shape=(1, 3)))
...
ArrowInvalid: Can only convert 1-dimensional array values

AFAIK, the only way to currently do this conversion, is to convert the list of arrays to a single ndarray yourself, and then use pa.FixedShapeTensorArray.from_numpy_ndarray.

(cc @rok)

Component(s)

Python

Activity

  1. jorisvandenbossche commented on Apr 2, 2026

    @jorisvandenbossche
    MemberAuthor

    And probably worth a separate issue, but also for the conversion the other way around to python, it would be nice to preserve the shape of the ndarrays:

    >>> arr = pa.FixedShapeTensorArray.from_numpy_ndarray(np.stack([np.array([[1,2,3], [3,4,5]]), np.array([[2,3,4], [5,6,7]])], axis=1))
    # results in an array of 1D arrays
    >>> arr.to_numpy(zero_copy_only=False)
    array([array([1, 2, 3, 2, 3, 4]), array([3, 4, 5, 5, 6, 7])], dtype=object)
    # the scalars do have a method to convert to the correct shape
    >>> [v.to_numpy() for v in arr]
    [array([[1, 2, 3],
            [2, 3, 4]]),
     array([[3, 4, 5],
            [5, 6, 7]])]
  2. rok commented on Apr 7, 2026

    @rok
    Member

    Agreed on both. Let's try to get this into the next release.

  3. added 11 commits that reference this issue on Jun 17, 2026
  4. added this to the 25.0.0 milestone on Jun 29, 2026
  5. added a commit that references this issue on Jun 29, 2026
  6. rok commented on Jun 29, 2026

    @rok
    Member

    @aboderinsamuel please write "take" in a comment below.

  7. aboderinsamuel commented on Jun 29, 2026

    @aboderinsamuel
    Contributor

    take

  8. added a commit that references this issue on Jun 29, 2026
  9. rok commented on Jun 29, 2026

    @rok
    Member

    Issue resolved by pull request 50203
    #50203

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

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions