Skip to content

Return scalar when accessing zero dimensional array #2718

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

Merged

Conversation

brokkoli71
Copy link
Member

@brokkoli71 brokkoli71 commented Jan 15, 2025

0-dimensional arrays are now returning a scalar. Therefore, the return type of __getitem__ changed
to NDArrayOrScalarLike. This change is to make the behavior of 0-dimensional arrays consistent with
numpy scalars.

TODO:

  • Add unit tests and/or doctests in docstrings
  • Add docstrings and API docs for any new/modified user-facing classes and functions
  • New/modified features documented in docs/user-guide/*.rst
  • Changes documented as a new file in changes/
  • GitHub Actions have all passed
  • Test coverage is 100% (Codecov passes)

@brokkoli71
Copy link
Member Author

brokkoli71 commented Jan 15, 2025

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

@brokkoli71 brokkoli71 marked this pull request as draft January 15, 2025 21:43
@dstansby
Copy link
Contributor

To copy what numpy does (see #2713 (comment)), I think we do want to return a scalar.

@github-actions github-actions bot added the needs release notes Automatically applied to PRs which haven't added release notes label Jan 29, 2025
@brokkoli71 brokkoli71 marked this pull request as ready for review February 12, 2025 14:48
@brokkoli71 brokkoli71 requested a review from d-v-b February 12, 2025 14:48
@brokkoli71
Copy link
Member Author

@d-v-b what do you think about NDArrayLikeOrScalar return type for __getitem__? (better ideas for the name of NDArrayLikeOrScalar are also welcome)

@normanrz normanrz enabled auto-merge (squash) April 10, 2025 13:32
@normanrz normanrz merged commit 06f7796 into zarr-developers:main Apr 10, 2025
30 checks passed
@brokkoli71 brokkoli71 deleted the return-scalar-for-zero-dim-indexing branch April 11, 2025 13:12
d-v-b pushed a commit to d-v-b/zarr-python that referenced this pull request Apr 20, 2025
…2718)

* Return scalar when accessing zero dimensional array

* returning npt.ArrayLike instead of NDArrayLike because of scalar return values

* returning npt.ArrayLike instead of NDArrayLike because of scalar return values

* fix mypy in tests

* fix mypy in tests

* fix mypy in tests

* improve test_scalar_array

* fix typo

* add ScalarWrapper

* use ScalarWrapper as NDArrayLike

* Revert "fix mypy in tests"

* Revert "fix mypy in tests"

This reverts commit 75d6cdf.

* Revert "fix mypy in tests"

This reverts commit 34bf260.

* format

* Revert "returning npt.ArrayLike instead of NDArrayLike because of scalar return values"

This reverts commit 1a290c7.

* Revert "returning npt.ArrayLike instead of NDArrayLike because of scalar return values"

This reverts commit 3348439

* fix mypy for ScalarWrapper

* add missing import NDArrayLike

* ignore unavoidable mypy error

* format

* fix __array__

* extend tests

* format

* fix typing in test_scalar_array

* add dtype to ScalarWrapper

* correct dtype type

* fix test_basic_indexing

* fix test_basic_indexing

* fix test_basic_indexing for dtype=datetime64[Y]

* increase codecov

* fix typing

* document changes

* move test_scalar_wrapper to test_buffer.py

* remove ScalarWrapper usage

* create NDArrayOrScalarLike

* fix NDArrayOrScalarLike

* fix mypy

* fix mypy

* fix mypy

* fix mypy in asynchronous.py

* fix mypy in test_api.py

* fix mypy in test_api.py and synchronous.py

* fix mypy in test_api.py and test_array.py

* fix mypy in test_array.py

* fix mypy in test_array.py

* fix mypy in test_array.py

* fix mypy in test_array.py

* fix mypy in test_array.py, test_api.py, test_buffer.py, test_sharding.py

* add bytes, str and datetime to ScalarType

* only support numpy datetime64 in ScalarType

* remove ScalarWrapper and update changes

* undo wrong code changes

* rename ``NDArrayOrScalarLike`` to ``NDArrayLikeOrScalar``

* rename ``NDArrayOrScalarLike`` to ``NDArrayLikeOrScalar``

* fix mypy in test_array.py

* fix mypy in test_array.py

* handle datetype scalars for different units

* fix mypy

* fix mypy

* format
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 this pull request may close these issues.

Inconsistent () indexing/size behavior from v2->v3
4 participants