Skip to content

Commit fc08f31

Browse files
d-v-bdcherian
andauthored
add init_array, and data kwarg for create_array (#2761)
* add init_array, and data kwarg for create_array * clean up some type hints and docstrings * add release notes * add tests for synchronous create_array * Update src/zarr/api/synchronous.py Co-authored-by: Deepak Cherian <[email protected]> * error if shape / dtype and data are provided --------- Co-authored-by: Deepak Cherian <[email protected]>
1 parent e602aa1 commit fc08f31

File tree

4 files changed

+419
-91
lines changed

4 files changed

+419
-91
lines changed

changes/2761.feature.1.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Adds a new function ``init_array`` for initializing an array in storage, and refactors ``create_array``
2+
to use ``init_array``. ``create_array`` takes two a new parameters: ``data``, an optional array-like object, and ``write_data``, a bool which defaults to ``True``.
3+
If ``data`` is given to ``create_array``, then the ``dtype`` and ``shape`` attributes of ``data`` are used to define the
4+
corresponding attributes of the resulting Zarr array. Additionally, if ``data`` given and ``write_data`` is ``True``,
5+
then the values in ``data`` will be written to the newly created array.

src/zarr/api/synchronous.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
if TYPE_CHECKING:
1515
from collections.abc import Iterable
1616

17+
import numpy as np
1718
import numpy.typing as npt
1819

1920
from zarr.abc.codec import Codec
@@ -744,8 +745,9 @@ def create_array(
744745
store: str | StoreLike,
745746
*,
746747
name: str | None = None,
747-
shape: ShapeLike,
748-
dtype: npt.DTypeLike,
748+
shape: ShapeLike | None = None,
749+
dtype: npt.DTypeLike | None = None,
750+
data: np.ndarray[Any, np.dtype[Any]] | None = None,
749751
chunks: ChunkCoords | Literal["auto"] = "auto",
750752
shards: ShardsLike | None = None,
751753
filters: FiltersLike = "auto",
@@ -772,10 +774,14 @@ def create_array(
772774
name : str or None, optional
773775
The name of the array within the store. If ``name`` is ``None``, the array will be located
774776
at the root of the store.
775-
shape : ChunkCoords
776-
Shape of the array.
777-
dtype : npt.DTypeLike
778-
Data type of the array.
777+
shape : ChunkCoords, optional
778+
Shape of the array. Can be ``None`` if ``data`` is provided.
779+
dtype : npt.DTypeLike, optional
780+
Data type of the array. Can be ``None`` if ``data`` is provided.
781+
data : np.ndarray, optional
782+
Array-like data to use for initializing the array. If this parameter is provided, the
783+
``shape`` and ``dtype`` parameters must be identical to ``data.shape`` and ``data.dtype``,
784+
or ``None``.
779785
chunks : ChunkCoords, optional
780786
Chunk shape of the array.
781787
If not specified, default are guessed based on the shape and dtype.
@@ -874,6 +880,7 @@ def create_array(
874880
name=name,
875881
shape=shape,
876882
dtype=dtype,
883+
data=data,
877884
chunks=chunks,
878885
shards=shards,
879886
filters=filters,

0 commit comments

Comments
 (0)