Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions python/pyarrow/array.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
18 changes: 17 additions & 1 deletion python/pyarrow/tests/test_extension_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading