Skip to content

Bytes array + ellipsis slicing -> IndexError: too many indices for array; expected 0, got 1 #3469

@cmalinmayor

Description

@cmalinmayor

Zarr version

v3.1.2

Numcodecs version

v0.16.2

Python Version

3.12

Operating System

Mac

Installation

uv/pip

Description

Related to #2436 but not exactly the same - trying to save a bytes array in a MemoryStore causes IndexError.

It does warn me that I should use bytes at my own risk 🙃 so perhaps it's not technically a bug? But a little more explanation on why bytes are risky would really be useful - the provided link wasn't very enlightening to me. We are implementing a standard file format ((geff)[http://liveimagetrackingtools.org/geff/latest/]) that we hope to be readable across Java and Python, and storing string arrays as byte arrays seemed to be the consensus for how to ensure readability across languages. Is this a bad idea?

Steps to reproduce

/// script
# requires-python = "==3.12"
# dependencies = ["zarr==3.1.2", "numpy"]
# ///
import zarr
import numpy as np
import numcodecs

def main() -> None:
    store = zarr.storage.MemoryStore()
    arr = np.array("teststr", dtype=np.bytes_)
    root = zarr.open(store)
    root["test"] = arr


if __name__ == "__main__":
    print(numcodecs.__version__)
    main()

malinmayorc@malinmayorc-lm1 scratch % uv run test_zarr.py
Installed 8 packages in 21ms
0.16.2
/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/dtype/npy/bytes.py:383: UnstableSpecificationWarning: The data type (NullTerminatedBytes(length=7)) does not have a Zarr V3 specification. That means that the representation of arrays saved with this data type may change without warning in a future version of Zarr Python. Arrays stored with this data type may be unreadable by other Zarr libraries. Use this data type at your own risk! Check https://github.com/zarr-developers/zarr-extensions/tree/main/data-types for the status of data type specifications for Zarr V3.
v3_unstable_dtype_warning(self)
Traceback (most recent call last):
File "/Users/malinmayorc/code/scratch/test_zarr.py", line 18, in
main()
File "/Users/malinmayorc/code/scratch/test_zarr.py", line 13, in main
root["test"] = arr
~~~~^^^^^^^^
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/group.py", line 2009, in setitem
self._sync(self._async_group.setitem(key, value))
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/sync.py", line 208, in _sync
return sync(
^^^^^
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/sync.py", line 163, in sync
raise return_result
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/sync.py", line 119, in _runner
return await coro
^^^^^^^^^^
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/group.py", line 692, in setitem
await async_api.save_array(
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/api/asynchronous.py", line 477, in save_array
await new.setitem(slice(None), arr)
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/array.py", line 1753, in setitem
indexer = BasicIndexer(
^^^^^^^^^^^^^
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/indexing.py", line 602, in init
selection_normalized = replace_ellipsis(selection, shape)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/indexing.py", line 523, in replace_ellipsis
check_selection_length(selection, shape)
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/indexing.py", line 488, in check_selection_length
err_too_many_indices(selection, shape)
File "/Users/malinmayorc/.cache/uv/archive-v0/kGUZcAWKHkSnbW4e_8QNm/lib/python3.12/site-packages/zarr/core/indexing.py", line 78, in err_too_many_indices
raise IndexError(f"too many indices for array; expected {len(shape)}, got {len(selection)}")
IndexError: too many indices for array; expected 0, got 1

Additional output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPotential issues with the zarr-python library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions