Skip to content

Change in indexing behaviour with numpy ints #967

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

Closed
benjeffery opened this issue Feb 16, 2022 · 3 comments · Fixed by #974
Closed

Change in indexing behaviour with numpy ints #967

benjeffery opened this issue Feb 16, 2022 · 3 comments · Fixed by #974

Comments

@benjeffery
Copy link
Contributor

benjeffery commented Feb 16, 2022

After 7c31f04 there was a breaking change to indexing behavior when a numpy integer type is passed:

For the following array:

z = zarr.group()
a = z.create_dataset("test",shape=(10,))
a[:]=range(10)

Before this commit:
a[np.int64(1)] has the value 1.0
After:
a[np.int64(1)] has the value [1.]

For both cases a[1] stays as 1.0 as expected. Was this change expected? This happens a lot when iterating over one numpy array and indexing with it as the iterable is of the numpy type. The fix in our application was to cast to a python int before indexing.

EDIT: Also note that numpy indexing returns the single value as was the case before this commit.

@raphaelquast
Copy link

I was just about to write the exact same issue... (stumbled upon it while trying to read an encoded dict)

root = zarr.group()
root.create("asdf", shape=(10,), dtype=object, object_codec=numcodecs.JSON())
root.asdf[0] = dict(a=1, b=2)

# using a normal python int to get the data
root.asdf[0]
#>>> {'a': 1, 'b': 2}

# using a numpy int to get the data
ids = np.array([0, 1, 2])
root.asdf[ids[0]]
#>>> array([{'a': 1, 'b': 2}], dtype=object)

benjeffery added a commit to benjeffery/zarr-python that referenced this issue Feb 28, 2022
benjeffery added a commit to benjeffery/zarr-python that referenced this issue Feb 28, 2022
joshmoore added a commit that referenced this issue Mar 7, 2022
* Fix indexing for scalar numpy values (#967)

* Fix linting errors

* Remove whitespace from GH editor

* Fix `W391 blank line at end of file`

Co-authored-by: Josh Moore <[email protected]>
Co-authored-by: jmoore <[email protected]>
@sanketverma1704
Copy link
Member

This issue has been fixed in patch release 2.11.1. 🎉

@benjeffery
Copy link
Contributor Author

This issue has been fixed in patch release 2.11.1. tada

Woop! Thankyou for the quick release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants