Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent () indexing/size behavior from v2->v3 #2713

Open
ilan-gold opened this issue Jan 15, 2025 · 5 comments · May be fixed by #2718
Open

Inconsistent () indexing/size behavior from v2->v3 #2713

ilan-gold opened this issue Jan 15, 2025 · 5 comments · May be fixed by #2718
Labels
bug Potential issues with the zarr-python library

Comments

@ilan-gold
Copy link
Contributor

Zarr version

v3.0.0

Numcodecs version

0.14.1

Python Version

3.12

Operating System

Mac

Installation

uv

Description

Previously something like z[()] where z is a () shape array would return a scalar and now returns a one-elem array e.g., np.array(1) instead of np.float64(1.0)

Steps to reproduce

v3:

import numpy as np
import zarr

z = zarr.open_group('foo.zarr')
z.create_array("bar", dtype=float, shape=())
z["bar"][...] = np.array(1) # why doesn't [:] work?
z["bar"][()] # array

v2:

import numpy as np
import zarr

z = zarr.open_group('foo.zarr')
z.create_dataset("bar", dtype=float, shape=(), data=np.array(1))
z["bar"][()] # np.float

Additional output

No response

@ilan-gold ilan-gold added the bug Potential issues with the zarr-python library label Jan 15, 2025
@ilan-gold ilan-gold changed the title Inconsisten () indexing/size behavior from v2->v3 Inconsistent () indexing/size behavior from v2->v3 Jan 15, 2025
@jhamman
Copy link
Member

jhamman commented Jan 15, 2025

Thanks @ilan-gold for the report. Looks like we missed the () indexer in #2583

@brokkoli71
Copy link
Member

I'll take a look at it.

regarding your comment in:
z["bar"][...] = np.array(1) # why doesn't [:] work?

The semantics of [:] is "select everything in the first dimension". I think since we have 0 dimensions this is expected to fail because there is no first dimension.

@brokkoli71 brokkoli71 linked a pull request Jan 15, 2025 that will close this issue
3 tasks
@ilan-gold
Copy link
Contributor Author

The semantics of [:] is "select everything in the first dimension". I think since we have 0 dimensions this is expected to fail because there is no first dimension

Makes sense!

@brokkoli71
Copy link
Member

Do we want to return scalars when accessing zero-dimensional arrays? This would mean that the return value of __getitem__ is no longer NDArrayLike and may make typing more difficult

@dstansby
Copy link
Contributor

Returning a scalar here would be consistent with numpy (2.2.1):

>>> import numpy as np
>>> a = np.array(1)
>>> a
array(1)
>>> a[()]
np.int64(1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Potential issues with the zarr-python library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants