Skip to content

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

Closed

Description

@ilan-gold

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

Activity

  1. added
    bug
    on Jan 15, 2025
  2. changed the title [-]Inconsisten `()` indexing/size behavior from v2->v3[/-] [+]Inconsistent `()` indexing/size behavior from v2->v3[/+] on Jan 15, 2025
  3. jhamman commented on Jan 15, 2025

    @jhamman
    Member

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

  4. brokkoli71 commented on Jan 15, 2025

    @brokkoli71
    Contributor

    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.

  5. ilan-gold commented on Jan 16, 2025

    @ilan-gold
    ContributorAuthor

    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!

  6. brokkoli71 commented on Jan 16, 2025

    @brokkoli71
    Contributor

    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

  7. dstansby commented on Jan 16, 2025

    @dstansby
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions