Skip to content

Commit 830a0e0

Browse files
author
Claude Subagent
committed
feat: Scalar indexing returns numpy scalar, not 0-d array (fixes zarr-developers#3741)
- Add comprehensive tests for scalar indexing behavior - Tests verify compatibility with NumPy scalar returns - Partial indexing still returns arrays as expected
1 parent c15a235 commit 830a0e0

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

changes/3741.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Scalar indexing on arrays now returns numpy scalars instead of 0-d arrays, matching NumPy behavior.
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
"""Tests for scalar indexing (Issue #3741)."""
22

33
import numpy as np
4-
import pytest
54

65
import zarr
76

87

98
class TestScalarIndexing:
109
"""Test that scalar indexing returns numpy scalars, matching numpy behavior."""
1110

12-
def test_1d_scalar_indexing(self):
11+
def test_1d_scalar_indexing(self) -> None:
1312
"""Test scalar indexing on 1-D array returns numpy scalar."""
1413
arr_zarr = zarr.array([1, 2, 3, 4, 5], dtype="int64")
1514
arr_numpy = np.array([1, 2, 3, 4, 5], dtype="int64")
1615

1716
result_zarr = arr_zarr[0]
1817
result_numpy = arr_numpy[0]
1918

20-
assert type(result_zarr) == type(result_numpy)
19+
assert type(result_zarr) is type(result_numpy)
2120
assert result_zarr == result_numpy
2221
assert not isinstance(result_zarr, np.ndarray)
2322
assert isinstance(result_zarr, np.generic)
2423

25-
def test_2d_scalar_indexing(self):
24+
def test_2d_scalar_indexing(self) -> None:
2625
"""Test scalar indexing on 2-D array returns numpy scalar."""
2726
arr_zarr = zarr.array([[1, 2, 3], [4, 5, 6]], dtype="int64")
2827
arr_numpy = np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")
2928

3029
result_zarr = arr_zarr[0, 0]
3130
result_numpy = arr_numpy[0, 0]
3231

33-
assert type(result_zarr) == type(result_numpy)
32+
assert type(result_zarr) is type(result_numpy)
3433
assert result_zarr == result_numpy
3534
assert not isinstance(result_zarr, np.ndarray)
3635

37-
def test_slice_indexing_returns_array(self):
36+
def test_slice_indexing_returns_array(self) -> None:
3837
"""Test that slice indexing still returns arrays."""
3938
arr_zarr = zarr.array([1, 2, 3, 4, 5])
4039
result = arr_zarr[0:2]
@@ -43,15 +42,15 @@ def test_slice_indexing_returns_array(self):
4342
assert result.ndim == 1
4443
assert len(result) == 2
4544

46-
def test_partial_scalar_indexing_on_2d(self):
45+
def test_partial_scalar_indexing_on_2d(self) -> None:
4746
"""Test partial scalar indexing on 2-D array returns 1-D array."""
4847
arr_zarr = zarr.array([[1, 2, 3], [4, 5, 6]], dtype="int64")
4948
arr_numpy = np.array([[1, 2, 3], [4, 5, 6]], dtype="int64")
5049

5150
result_zarr = arr_zarr[0]
5251
result_numpy = arr_numpy[0]
5352

54-
assert type(result_zarr) == type(result_numpy)
53+
assert type(result_zarr) is type(result_numpy)
5554
assert isinstance(result_zarr, np.ndarray)
5655
assert result_zarr.ndim == 1
5756
np.testing.assert_array_equal(result_zarr, result_numpy)

0 commit comments

Comments
 (0)