diff --git a/python/pyarrow/array.pxi b/python/pyarrow/array.pxi index d45c3470f8c..e1b74dcf602 100644 --- a/python/pyarrow/array.pxi +++ b/python/pyarrow/array.pxi @@ -277,15 +277,20 @@ def array(object obj, type=None, mask=None, size=None, from_pandas=None, # tensor-specific constraints here where the type is still known. if (isinstance(extension_type, FixedShapeTensorType) and isinstance(obj, (list, tuple))): - if extension_type.permutation is not None: - raise NotImplementedError( - "Converting a sequence of arrays to a fixed_shape_tensor " - "with a permutation is not supported") + permutation = extension_type.permutation + is_permuted = (permutation is not None + and list(permutation) != list(range(len(permutation)))) expected_shape = tuple(extension_type.shape) for element in obj: shape = getattr(element, "shape", None) - if (shape is not None and len(shape) >= 2 - and tuple(shape) != expected_shape): + if shape is None or len(shape) < 2: + continue + if is_permuted: + raise ArrowNotImplementedError( + "Converting a sequence of arrays to a " + "fixed_shape_tensor with a permutation is not " + "supported") + if tuple(shape) != expected_shape: raise ValueError( f"Cannot convert array of shape {tuple(shape)} to a " f"fixed_shape_tensor of shape {expected_shape}") diff --git a/python/pyarrow/tests/test_extension_type.py b/python/pyarrow/tests/test_extension_type.py index a9d15cfec10..b6b6df8d1aa 100644 --- a/python/pyarrow/tests/test_extension_type.py +++ b/python/pyarrow/tests/test_extension_type.py @@ -1869,10 +1869,26 @@ def test_tensor_array_from_list_of_ndarrays(np_type_str): permuted_type = pa.fixed_shape_tensor( pa.from_numpy_dtype(np_dtype), (2, 3), permutation=[1, 0]) - with pytest.raises(NotImplementedError, match="permutation"): + with pytest.raises(pa.ArrowNotImplementedError, match="permutation"): pa.array(elements, type=permuted_type) +@pytest.mark.parametrize("values", ([], [None], [[0, 1, 2, 3]])) +def test_tensor_array_from_list_with_permutation(values): + tensor_type = pa.fixed_shape_tensor(pa.int32(), (2, 2), permutation=[1, 0]) + result = pa.array(values, type=tensor_type) + assert result.type == tensor_type + + +@pytest.mark.numpy +def test_tensor_array_from_list_with_identity_permutation(): + tensor_type = pa.fixed_shape_tensor(pa.int64(), (2, 3), permutation=[0, 1]) + elements = [np.arange(6).reshape(2, 3)] + result = pa.array(elements, type=tensor_type) + assert result.type == tensor_type + np.testing.assert_array_equal(result[0].to_numpy(), elements[0]) + + @pytest.mark.numpy def test_tensor_array_from_list_mixed_layout(): # C- and F-ordered arrays with the same values must produce the same