Skip to content

Fix a regression with scalar indexing due to #1800 #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Docs

Maintenance
~~~~~~~~~~~
* Fix a regression when getting or setting a single value from arrays with size-1 chunks.
By :user:`Deepak Cherian <dcherian>` :issue:`1874`

Deprecations
~~~~~~~~~~~~
Expand Down
11 changes: 8 additions & 3 deletions zarr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,9 @@ def _process_chunk(
and not self._filters
and self._dtype != object
):
dest = out[out_selection]
# For 0D arrays out_selection = () and out[out_selection] is a scalar
# Avoid that
dest = out[out_selection] if out_selection else out
# Assume that array-like objects that doesn't have a
# `writeable` flag is writable.
dest_is_writable = getattr(dest, "writeable", True)
Expand Down Expand Up @@ -2281,8 +2283,11 @@ def _process_for_setitem(self, ckey, chunk_selection, value, fields=None):
chunk.fill(value)

else:
# ensure array is contiguous
chunk = value.astype(self._dtype, order=self._order, copy=False)
if not isinstance(value, np.ndarray):
chunk = np.asarray(value, dtype=self._dtype, order=self._order)
else:
# ensure array is contiguous
chunk = value.astype(self._dtype, order=self._order, copy=False)

else:
# partially replace the contents of this chunk
Expand Down
19 changes: 19 additions & 0 deletions zarr/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3157,3 +3157,22 @@ def test_issue_1279(tmpdir):

written_data = ds_reopened[:]
assert_array_equal(data, written_data)


def test_scalar_indexing():
store = zarr.KVStore({})

store["a"] = zarr.create((3,), chunks=(1,), store=store)
store["a"][:] = [1, 2, 3]

assert store["a"][1] == np.array(2.0)
assert store["a"][(1,)] == np.array(2.0)

store["a"][0] = [-1]
assert store["a"][0] == np.array(-1)

store["a"][0] = -2
assert store["a"][0] == np.array(-2)

store["a"][0] = (-3,)
assert store["a"][0] == np.array(-3)