Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2881,6 +2881,14 @@ def set_coordinate_selection(
if hasattr(value, "shape") and len(value.shape) > 1:
value = np.array(value).reshape(-1)

if not is_scalar(value, self.dtype) and (
isinstance(value, NDArrayLike) and indexer.shape != value.shape
):
raise ValueError(
f"Attempting to set a selection of {indexer.sel_shape[0]} "
f"elements with an array of {value.shape[0]} elements."
)

sync(self._async_array._set_selection(indexer, value, fields=fields, prototype=prototype))

@_deprecate_positional_args
Expand Down
18 changes: 18 additions & 0 deletions tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1936,3 +1936,21 @@ def test_zero_sized_chunks(store: StorePath, shape: list[int]) -> None:
z = Array.create(store=store, shape=shape, chunk_shape=shape, zarr_format=3, dtype="f8")
z[...] = 42
assert_array_equal(z[...], np.zeros(shape, dtype="f8"))


@pytest.mark.parametrize("store", ["memory"], indirect=["store"])
def test_vectorized_indexing_incompatible_shape(store) -> None:
# GH2469
shape = (4, 4)
chunks = (2, 2)
fill_value = 32767
arr = zarr.create(
shape,
store=store,
chunks=chunks,
dtype=np.int16,
fill_value=fill_value,
codecs=[zarr.codecs.BytesCodec(), zarr.codecs.BloscCodec()],
)
with pytest.raises(ValueError, match="Attempting to set"):
arr[np.array([1, 2]), np.array([1, 2])] = np.array([[-1, -2], [-3, -4]])