Skip to content

 zarr.create

zarr.create

create(
    shape: tuple[int, ...] | int,
    *,
    chunks: ChunksLike | None = None,
    dtype: ZDTypeLike | None = None,
    compressor: CompressorLike = "auto",
    fill_value: Any | None = DEFAULT_FILL_VALUE,
    order: MemoryOrder | None = None,
    store: StoreLike | None = None,
    synchronizer: Any | None = None,
    overwrite: bool = False,
    path: PathLike | None = None,
    chunk_store: StoreLike | None = None,
    filters: Iterable[dict[str, JSON] | Numcodec]
    | None = None,
    cache_metadata: bool | None = None,
    cache_attrs: bool | None = None,
    read_only: bool | None = None,
    object_codec: Codec | None = None,
    dimension_separator: Literal[".", "/"] | None = None,
    write_empty_chunks: bool | None = None,
    zarr_format: ZarrFormat | None = None,
    meta_array: Any | None = None,
    attributes: dict[str, JSON] | None = None,
    chunk_shape: ChunksLike | None = None,
    chunk_key_encoding: ChunkKeyEncoding
    | tuple[Literal["default"], Literal[".", "/"]]
    | tuple[Literal["v2"], Literal[".", "/"]]
    | None = None,
    codecs: Iterable[Codec | dict[str, JSON]] | None = None,
    dimension_names: DimensionNamesLike = None,
    storage_options: dict[str, Any] | None = None,
    config: ArrayConfigLike | None = None,
    mode: AccessModeLiteral | None = None,
    data: ArrayLike | None = None,
) -> AnyArray

Create an array.

Parameters:

  • shape (int or tuple of ints) –

    Array shape.

  • chunks (ChunksLike, default: None ) –

    Chunk shape. If None (the default), it is guessed from shape and dtype. If False, it is set to shape, i.e., a single chunk for the whole array. If an int, the chunk size in each dimension is given by the value of chunks. True is not a chunk shape and raises a ValueError.

  • dtype (str or dtype, default: None ) –

    NumPy dtype.

  • compressor (Codec, default: 'auto' ) –

    Primary compressor to compress chunk data. Zarr format 2 only. Zarr format 3 arrays should use codecs instead.

    If neither compressor nor filters are provided, the default compressor zarr.codecs.ZstdCodec is used.

    If compressor is set to None, no compression is used.

  • fill_value (Any, default: DEFAULT_FILL_VALUE ) –

    Fill value for the array.

  • order (('C', 'F'), default: 'C' ) –

    Deprecated in favor of the config keyword argument. Pass {'order': <value>} to create instead of using this parameter. Memory layout to be used within each chunk. If not specified, the array.order parameter in the global config will be used.

  • store (StoreLike or None, default: None ) –

    StoreLike object to open. See the storage documentation in the user guide for a description of all valid StoreLike values.

  • synchronizer (object, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • overwrite (bool, default: False ) –

    If True, delete all pre-existing data in store at path before creating the array.

  • path (str, default: None ) –

    Path under which array is stored.

  • chunk_store (StoreLike or None, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • filters (Iterable[Codec] | Literal['auto'], default: None ) –

    Iterable of filters to apply to each chunk of the array, in order, before serializing that chunk to bytes.

    For Zarr format 3, a "filter" is a codec that takes an array and returns an array, and these values must be instances of zarr.abc.codec.ArrayArrayCodec, or a dict representations of zarr.abc.codec.ArrayArrayCodec.

    For Zarr format 2, a "filter" can be any numcodecs codec; you should ensure that the order of your filters is consistent with the behavior of each filter.

    The default value of "auto" instructs Zarr to use a default based on the data type of the array and the Zarr format specified. For all data types in Zarr V3, and most data types in Zarr V2, the default filters are empty. The only cases where default filters are not empty is when the Zarr format is 2, and the data type is a variable-length data type like zarr.dtype.VariableLengthUTF8 or zarr.dtype.VariableLengthBytes. In these cases, the default filters contains a single element which is a codec specific to that particular data type.

    To create an array with no filters, provide an empty iterable or the value None.

  • cache_metadata (bool, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • cache_attrs (bool, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • read_only (bool, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • object_codec (Codec, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • dimension_separator (('.', '/'), default: '.' ) –

    Separator placed between the dimensions of a chunk. Zarr format 2 only. Zarr format 3 arrays should use chunk_key_encoding instead.

  • write_empty_chunks (bool, default: None ) –

    Deprecated in favor of the config keyword argument. Pass {'write_empty_chunks': <value>} to create instead of using this parameter. If True, all chunks will be stored regardless of their contents. If False, each chunk is compared to the array's fill value prior to storing. If a chunk is uniformly equal to the fill value, then that chunk is not be stored, and the store entry for that chunk's key is deleted.

  • zarr_format ((2, 3, None), default: 2 ) –

    The Zarr format to use when creating an array. The default is None, which instructs Zarr to choose the default Zarr format value defined in the runtime configuration.

  • meta_array (array - like, default: None ) –

    Ignored. Deprecated and will be removed in a release after February 2027.

  • attributes (dict[str, JSON], default: None ) –

    A dictionary of user attributes to store with the array.

  • chunk_shape (ChunksLike, default: None ) –

    The shape of the Array's chunks (default is None). Zarr format 3 only. Zarr format 2 arrays should use chunks instead.

  • chunk_key_encoding (ChunkKeyEncoding, default: None ) –

    A specification of how the chunk keys are represented in storage. Zarr format 3 only. Zarr format 2 arrays should use dimension_separator instead. Default is ("default", "/").

  • codecs (Sequence of Codecs or dicts, default: None ) –

    An iterable of Codec or dict serializations of Codecs. Zarr V3 only.

    The elements of codecs specify the transformation from array values to stored bytes. Zarr format 3 only. Zarr format 2 arrays should use filters and compressor instead. The Zarr format 3 specification requires, in order, zero or more array -> array codecs (zarr.abc.codec.ArrayArrayCodec), exactly one array -> bytes codec (zarr.abc.codec.ArrayBytesCodec), and zero or more bytes -> bytes codecs (zarr.abc.codec.BytesBytesCodec). Elsewhere in Zarr-Python these are called filters, the serializer, and compressors; those names are Zarr-Python aliases, not terms defined in the specification.

    If no codecs are provided, default codecs will be used based on the data type of the array. For most data types, the default codecs are the tuple (BytesCodec(), ZstdCodec()); data types that require a special zarr.abc.codec.ArrayBytesCodec, like variable-length strings or bytes, will use the zarr.abc.codec.ArrayBytesCodec required for the data type instead of zarr.codecs.BytesCodec.

  • dimension_names (Iterable[str | None] | None = None, default: None ) –

    An iterable of dimension names. Zarr format 3 only.

  • storage_options (dict, default: None ) –

    If using an fsspec URL to create the store, these will be passed to the backend implementation. Ignored otherwise.

  • config (ArrayConfigLike, default: None ) –

    Runtime configuration of the array. If provided, will override the default values from zarr.config.array.

  • mode (('r', 'r+', 'a', 'w', 'w-'), default: 'r' ) –

    Legacy way to control overwriting, kept for compatibility with Zarr-Python 2. Prefer overwrite. The access mode used to open store; the default, None, is 'a'.

    • 'a' and 'r+' create the array and fail if a node exists at path, unless overwrite is True.
    • 'w' replaces anything stored under path (the whole store if path is not set), even if overwrite is False. On a store that cannot delete keys, 'w' raises an error instead of replacing an existing node.
    • 'w-' fails if anything is stored under path, even if overwrite is True.
    • 'r' always fails.

    If store is a StorePath, mode is not validated against it: 'w' still sets overwrite, and the other modes have no effect.

  • data (array - like, default: None ) –

    Values written into the new array after it is created. Unlike the data parameter of create_array, it does not set the shape or data type of the array. To create an array from existing data, use create_array(data=...). A Zarr array is not supported as data.

Returns:

  • z ( Array ) –

    The array.

Source code in src/zarr/api/synchronous.py
def create(
    shape: tuple[int, ...] | int,
    *,  # Note: this is a change from v2
    chunks: ChunksLike | None = None,
    dtype: ZDTypeLike | None = None,
    compressor: CompressorLike = "auto",
    fill_value: Any | None = DEFAULT_FILL_VALUE,  # TODO: need type
    order: MemoryOrder | None = None,
    store: StoreLike | None = None,
    synchronizer: Any | None = None,
    overwrite: bool = False,
    path: PathLike | None = None,
    chunk_store: StoreLike | None = None,
    filters: Iterable[dict[str, JSON] | Numcodec] | None = None,
    cache_metadata: bool | None = None,
    cache_attrs: bool | None = None,
    read_only: bool | None = None,
    object_codec: Codec | None = None,  # TODO: type has changed
    dimension_separator: Literal[".", "/"] | None = None,
    write_empty_chunks: bool | None = None,  # TODO: default has changed
    zarr_format: ZarrFormat | None = None,
    meta_array: Any | None = None,  # TODO: need type
    attributes: dict[str, JSON] | None = None,
    # v3 only
    chunk_shape: ChunksLike | None = None,
    chunk_key_encoding: (
        ChunkKeyEncoding
        | tuple[Literal["default"], Literal[".", "/"]]
        | tuple[Literal["v2"], Literal[".", "/"]]
        | None
    ) = None,
    codecs: Iterable[Codec | dict[str, JSON]] | None = None,
    dimension_names: DimensionNamesLike = None,
    storage_options: dict[str, Any] | None = None,
    config: ArrayConfigLike | None = None,
    mode: AccessModeLiteral | None = None,
    data: npt.ArrayLike | None = None,
) -> AnyArray:
    """Create an array.

    Parameters
    ----------
    shape : int or tuple of ints
        Array shape.
    chunks : ChunksLike, optional
        Chunk shape. If None (the default), it is guessed from `shape` and `dtype`. If
        False, it is set to `shape`, i.e., a single chunk for the whole array. If an
        int, the chunk size in each dimension is given by the value of `chunks`.
        `True` is not a chunk shape and raises a `ValueError`.
    dtype : str or dtype, optional
        NumPy dtype.
    compressor : Codec, optional
        Primary compressor to compress chunk data.
        Zarr format 2 only. Zarr format 3 arrays should use `codecs` instead.

        If neither `compressor` nor `filters` are provided, the default compressor
        [`zarr.codecs.ZstdCodec`][] is used.

        If `compressor` is set to `None`, no compression is used.
    fill_value : Any, optional
        Fill value for the array.
    order : {'C', 'F'}, optional
        Deprecated in favor of the `config` keyword argument.
        Pass `{'order': <value>}` to `create` instead of using this parameter.
        Memory layout to be used within each chunk.
        If not specified, the `array.order` parameter in the global config will be used.
    store : StoreLike or None, default=None
        StoreLike object to open. See the
        [storage documentation in the user guide][user-guide-store-like]
        for a description of all valid StoreLike values.
    synchronizer : object, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    overwrite : bool, optional
        If True, delete all pre-existing data in `store` at `path` before
        creating the array.
    path : str, optional
        Path under which array is stored.
    chunk_store : StoreLike or None, default=None
        Ignored. Deprecated and will be removed in a release after February 2027.
    filters : Iterable[Codec] | Literal["auto"], optional
        Iterable of filters to apply to each chunk of the array, in order, before serializing that
        chunk to bytes.

        For Zarr format 3, a "filter" is a codec that takes an array and returns an array,
        and these values must be instances of [`zarr.abc.codec.ArrayArrayCodec`][], or a
        dict representations of [`zarr.abc.codec.ArrayArrayCodec`][].

        For Zarr format 2, a "filter" can be any numcodecs codec; you should ensure that the
        order of your filters is consistent with the behavior of each filter.

        The default value of `"auto"` instructs Zarr to use a default based on the data
        type of the array and the Zarr format specified. For all data types in Zarr V3, and most
        data types in Zarr V2, the default filters are empty. The only cases where default filters
        are not empty is when the Zarr format is 2, and the data type is a variable-length data type like
        [`zarr.dtype.VariableLengthUTF8`][] or [`zarr.dtype.VariableLengthBytes`][]. In these cases,
        the default filters contains a single element which is a codec specific to that particular data type.

        To create an array with no filters, provide an empty iterable or the value `None`.
    cache_metadata : bool, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    cache_attrs : bool, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    read_only : bool, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    object_codec : Codec, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    dimension_separator : {'.', '/'}, optional
        Separator placed between the dimensions of a chunk.
        Zarr format 2 only. Zarr format 3 arrays should use `chunk_key_encoding` instead.
    write_empty_chunks : bool, optional
        Deprecated in favor of the `config` keyword argument.
        Pass `{'write_empty_chunks': <value>}` to `create` instead of using this parameter.
        If True, all chunks will be stored regardless of their
        contents. If False, each chunk is compared to the array's fill value
        prior to storing. If a chunk is uniformly equal to the fill value, then
        that chunk is not be stored, and the store entry for that chunk's key
        is deleted.
    zarr_format : {2, 3, None}, optional
        The Zarr format to use when creating an array. The default is `None`,
        which instructs Zarr to choose the default Zarr format value defined in the
        runtime configuration.
    meta_array : array-like, optional
        Ignored. Deprecated and will be removed in a release after February 2027.
    attributes : dict[str, JSON], optional
        A dictionary of user attributes to store with the array.
    chunk_shape : ChunksLike, optional
        The shape of the Array's chunks (default is None).
        Zarr format 3 only. Zarr format 2 arrays should use `chunks` instead.
    chunk_key_encoding : ChunkKeyEncoding, optional
        A specification of how the chunk keys are represented in storage.
        Zarr format 3 only. Zarr format 2 arrays should use `dimension_separator` instead.
        Default is `("default", "/")`.
    codecs : Sequence of Codecs or dicts, optional
        An iterable of Codec or dict serializations of Codecs. Zarr V3 only.

        The elements of `codecs` specify the transformation from array values to stored bytes.
        Zarr format 3 only. Zarr format 2 arrays should use `filters` and `compressor` instead.
        The [Zarr format 3 specification](https://zarr-specs.readthedocs.io/en/latest/v3/core/index.html#chunk-encoding)
        requires, in order, zero or more array -> array codecs
        ([`zarr.abc.codec.ArrayArrayCodec`][]), exactly one array -> bytes codec
        ([`zarr.abc.codec.ArrayBytesCodec`][]), and zero or more bytes -> bytes codecs
        ([`zarr.abc.codec.BytesBytesCodec`][]). Elsewhere in Zarr-Python these are
        called filters, the serializer, and compressors; those names are Zarr-Python
        aliases, not terms defined in the specification.

        If no codecs are provided, default codecs will be used based on the data type of the array.
        For most data types, the default codecs are the tuple `(BytesCodec(), ZstdCodec())`;
        data types that require a special [`zarr.abc.codec.ArrayBytesCodec`][], like variable-length strings or bytes,
        will use the [`zarr.abc.codec.ArrayBytesCodec`][] required for the data type instead of [`zarr.codecs.BytesCodec`][].
    dimension_names : Iterable[str | None] | None = None
        An iterable of dimension names. Zarr format 3 only.
    storage_options : dict
        If using an fsspec URL to create the store, these will be passed to
        the backend implementation. Ignored otherwise.
    config : ArrayConfigLike, optional
        Runtime configuration of the array. If provided, will override the
        default values from `zarr.config.array`.
    mode : {'r', 'r+', 'a', 'w', 'w-'}, optional
        Legacy way to control overwriting, kept for compatibility with Zarr-Python 2.
        Prefer `overwrite`. The access mode used to open `store`; the default, `None`,
        is `'a'`.

        - `'a'` and `'r+'` create the array and fail if a node exists at `path`,
          unless `overwrite` is `True`.
        - `'w'` replaces anything stored under `path` (the whole store if `path` is
          not set), even if `overwrite` is `False`. On a store that cannot delete
          keys, `'w'` raises an error instead of replacing an existing node.
        - `'w-'` fails if anything is stored under `path`, even if `overwrite` is
          `True`.
        - `'r'` always fails.

        If `store` is a `StorePath`, `mode` is not validated against it: `'w'` still
        sets `overwrite`, and the other modes have no effect.
    data : array-like, optional
        Values written into the new array after it is created. Unlike the `data`
        parameter of `create_array`, it does not set the shape or data type of the
        array. To create an array from existing data, use `create_array(data=...)`.
        A Zarr array is not supported as `data`.

    Returns
    -------
    z : Array
        The array.
    """
    return Array(
        sync(
            async_api.create(
                shape=shape,
                chunks=chunks,
                dtype=dtype,
                compressor=compressor,
                fill_value=fill_value,
                order=order,
                store=store,
                synchronizer=synchronizer,
                overwrite=overwrite,
                path=path,
                chunk_store=chunk_store,
                filters=filters,
                cache_metadata=cache_metadata,
                cache_attrs=cache_attrs,
                read_only=read_only,
                object_codec=object_codec,
                dimension_separator=dimension_separator,
                write_empty_chunks=write_empty_chunks,
                zarr_format=zarr_format,
                meta_array=meta_array,
                attributes=attributes,
                chunk_shape=chunk_shape,
                chunk_key_encoding=chunk_key_encoding,
                codecs=codecs,
                dimension_names=dimension_names,
                storage_options=storage_options,
                config=config,
                mode=mode,
                data=data,
            )
        )
    )