Skip to content

Commit b5b92af

Browse files
committed
support zero-sized chunks
1 parent 8a33df7 commit b5b92af

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/zarr/core/indexing.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ def __iter__(self) -> Iterator[ChunkProjection]: ...
9494

9595

9696
def ceildiv(a: float, b: float) -> int:
97+
if a == 0:
98+
return 0
9799
return math.ceil(a / b)
98100

99101

@@ -374,7 +376,7 @@ def __init__(self, dim_sel: slice, dim_len: int, dim_chunk_len: int) -> None:
374376

375377
def __iter__(self) -> Iterator[ChunkDimProjection]:
376378
# figure out the range of chunks we need to visit
377-
dim_chunk_ix_from = self.start // self.dim_chunk_len
379+
dim_chunk_ix_from = 0 if self.start == 0 else self.start // self.dim_chunk_len
378380
dim_chunk_ix_to = ceildiv(self.stop, self.dim_chunk_len)
379381

380382
# iterate over chunks in range

tests/test_indexing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from numpy.testing import assert_array_equal
1212

1313
import zarr
14+
from zarr import Array
1415
from zarr.core.buffer import BufferPrototype, default_buffer_prototype
1516
from zarr.core.indexing import (
1617
BasicSelection,
@@ -1927,3 +1928,11 @@ def test_indexing_with_zarr_array(store: StorePath) -> None:
19271928

19281929
assert_array_equal(a[ii], za[zii])
19291930
assert_array_equal(a[ii], za.oindex[zii])
1931+
1932+
1933+
@pytest.mark.parametrize("store", ["local", "memory"], indirect=["store"])
1934+
@pytest.mark.parametrize("shape", [(0, 2, 3), (0), (3, 0)])
1935+
def test_zero_sized_chunks(store: StorePath, shape: list[int]) -> None:
1936+
z = Array.create(store=store, shape=shape, chunk_shape=shape, zarr_format=3, dtype="f8")
1937+
z[...] = 42
1938+
assert_array_equal(z[...], np.zeros(shape, dtype="f8"))

0 commit comments

Comments
 (0)