Skip to content

Commit 908e920

Browse files
authored
Fix a regression with scalar indexing due to #1800 (#1875)
1 parent 8264ace commit 908e920

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

docs/release.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Docs
3131

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

3537
Deprecations
3638
~~~~~~~~~~~~

zarr/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,9 @@ def _process_chunk(
20302030
and not self._filters
20312031
and self._dtype != object
20322032
):
2033-
dest = out[out_selection]
2033+
# For 0D arrays out_selection = () and out[out_selection] is a scalar
2034+
# Avoid that
2035+
dest = out[out_selection] if out_selection else out
20342036
# Assume that array-like objects that doesn't have a
20352037
# `writeable` flag is writable.
20362038
dest_is_writable = getattr(dest, "writeable", True)

zarr/indexing.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def is_scalar(value, dtype):
5252
return True
5353
if isinstance(value, tuple) and dtype.names and len(value) == len(dtype.names):
5454
return True
55+
if dtype.kind == "O" and not isinstance(value, np.ndarray):
56+
return True
5557
return False
5658

5759

zarr/tests/test_core.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3157,3 +3157,52 @@ def test_issue_1279(tmpdir):
31573157

31583158
written_data = ds_reopened[:]
31593159
assert_array_equal(data, written_data)
3160+
3161+
3162+
def test_scalar_indexing():
3163+
store = zarr.KVStore({})
3164+
3165+
store["a"] = zarr.create((3,), chunks=(1,), store=store)
3166+
store["a"][:] = [1, 2, 3]
3167+
3168+
assert store["a"][1] == np.array(2.0)
3169+
assert store["a"][(1,)] == np.array(2.0)
3170+
3171+
store["a"][slice(1)] = [-1]
3172+
assert store["a"][0] == np.array(-1)
3173+
3174+
store["a"][0] = -2
3175+
assert store["a"][0] == np.array(-2)
3176+
3177+
store["a"][slice(1)] = (-3,)
3178+
assert store["a"][0] == np.array(-3)
3179+
3180+
3181+
def test_object_array_indexing():
3182+
# regression test for #1874
3183+
from numcodecs import MsgPack
3184+
3185+
root = zarr.group()
3186+
arr = root.create_dataset(
3187+
name="my_dataset",
3188+
shape=0,
3189+
dtype=object,
3190+
object_codec=MsgPack(),
3191+
)
3192+
new_items = [
3193+
["A", 1],
3194+
["B", 2, "hello"],
3195+
]
3196+
arr_add = np.empty(len(new_items), dtype=object)
3197+
arr_add[:] = new_items
3198+
arr.append(arr_add)
3199+
3200+
# heterogeneous elements
3201+
elem = ["C", 3]
3202+
arr[0] = elem
3203+
assert arr[0] == elem
3204+
3205+
# homogeneous elements
3206+
elem = [1, 3]
3207+
arr[1] = elem
3208+
assert arr[1] == elem

0 commit comments

Comments
 (0)