Skip to content

Commit 58b1786

Browse files
authored
Use ensure_ndarray in a few more places (#506)
* Use `ensure_ndarray` to check `chunk`'s type Instead of checking to see if `chunk` is an `ndarray`, use `ensure_ndarray` to get an `ndarray` viewing the underlying data in `chunk`. This way we know attributes like `dtype` are available and can be checked easily. Also makes this a bit more friendly with other array-like types. * Use `ensure_ndarray` on `out` Since we are interested in getting an `ndarray` representing the buffer within `out` for writing into, go ahead and use `ensure_ndarray` to coerce the underlying buffer into an `ndarray`. This way we can avoid a needless check and just write into any array-like value for `out` that is provided. * Handle `out` when it doesn't supply a buffer Appears that `out` can also be a Zarr `Array` or any other array-like that does not expose a buffer, but does allow writing into. In these cases `ensure_ndarray` will fail as there is not an underlying buffer that can be used with an `ndarray`. To also handle this case, catch the `TypeError` that `ensure_ndarray` will raise in this case and use that to indicate whether `out` is now an `ndarray` or not. This allows us to continue to write into arbitrary buffers, but also correctly handle objects that do not expose buffers. * Adds release notes entry
1 parent f5718c5 commit 58b1786

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

docs/release.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Upcoming Release
4040
* Use ``math.ceil`` for scalars.
4141
By :user:`John Kirkham <jakirkham>`; :issue:`500`.
4242

43+
* Use ``ensure_ndarray`` in a few more places.
44+
By :user:`John Kirkham <jakirkham>`; :issue:`506`.
45+
4346
* Refactor out ``_tofile``/``_fromfile`` from ``DirectoryStore``.
4447
By :user:`John Kirkham <jakirkham>`; :issue:`503`.
4548

zarr/core.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,12 @@ def _chunk_getitem(self, chunk_coords, chunk_selection, out, out_selection,
15721572

15731573
assert len(chunk_coords) == len(self._cdata_shape)
15741574

1575+
out_is_ndarray = True
1576+
try:
1577+
out = ensure_ndarray(out)
1578+
except TypeError:
1579+
out_is_ndarray = False
1580+
15751581
# obtain key for chunk
15761582
ckey = self._chunk_key(chunk_coords)
15771583

@@ -1590,7 +1596,7 @@ def _chunk_getitem(self, chunk_coords, chunk_selection, out, out_selection,
15901596

15911597
else:
15921598

1593-
if (isinstance(out, np.ndarray) and
1599+
if (out_is_ndarray and
15941600
not fields and
15951601
is_contiguous_selection(out_selection) and
15961602
is_total_slice(chunk_selection, self._chunks) and
@@ -1769,7 +1775,7 @@ def _encode_chunk(self, chunk):
17691775
chunk = f.encode(chunk)
17701776

17711777
# check object encoding
1772-
if isinstance(chunk, np.ndarray) and chunk.dtype == object:
1778+
if ensure_ndarray(chunk).dtype == object:
17731779
raise RuntimeError('cannot write object array without object codec')
17741780

17751781
# compress

0 commit comments

Comments
 (0)