3636from zarr .core .buffer import (
3737 BufferPrototype ,
3838 NDArrayLike ,
39+ NDArrayLikeOrScalar ,
3940 NDBuffer ,
4041 default_buffer_prototype ,
4142)
@@ -1257,7 +1258,7 @@ async def _get_selection(
12571258 prototype : BufferPrototype ,
12581259 out : NDBuffer | None = None ,
12591260 fields : Fields | None = None ,
1260- ) -> NDArrayLike :
1261+ ) -> NDArrayLikeOrScalar :
12611262 # check fields are sensible
12621263 out_dtype = check_fields (fields , self .dtype )
12631264
@@ -1299,14 +1300,16 @@ async def _get_selection(
12991300 out_buffer ,
13001301 drop_axes = indexer .drop_axes ,
13011302 )
1303+ if isinstance (indexer , BasicIndexer ) and indexer .shape == ():
1304+ return out_buffer .as_scalar ()
13021305 return out_buffer .as_ndarray_like ()
13031306
13041307 async def getitem (
13051308 self ,
13061309 selection : BasicSelection ,
13071310 * ,
13081311 prototype : BufferPrototype | None = None ,
1309- ) -> NDArrayLike :
1312+ ) -> NDArrayLikeOrScalar :
13101313 """
13111314 Asynchronous function that retrieves a subset of the array's data based on the provided selection.
13121315
@@ -1319,7 +1322,7 @@ async def getitem(
13191322
13201323 Returns
13211324 -------
1322- NDArrayLike
1325+ NDArrayLikeOrScalar
13231326 The retrieved subset of the array's data.
13241327
13251328 Examples
@@ -2269,14 +2272,15 @@ def __array__(
22692272 msg = "`copy=False` is not supported. This method always creates a copy."
22702273 raise ValueError (msg )
22712274
2272- arr_np = self [...]
2275+ arr = self [...]
2276+ arr_np : NDArrayLike = np .array (arr , dtype = dtype )
22732277
22742278 if dtype is not None :
22752279 arr_np = arr_np .astype (dtype )
22762280
22772281 return arr_np
22782282
2279- def __getitem__ (self , selection : Selection ) -> NDArrayLike :
2283+ def __getitem__ (self , selection : Selection ) -> NDArrayLikeOrScalar :
22802284 """Retrieve data for an item or region of the array.
22812285
22822286 Parameters
@@ -2287,8 +2291,8 @@ def __getitem__(self, selection: Selection) -> NDArrayLike:
22872291
22882292 Returns
22892293 -------
2290- NDArrayLike
2291- An array-like containing the data for the requested region.
2294+ NDArrayLikeOrScalar
2295+ An array-like or scalar containing the data for the requested region.
22922296
22932297 Examples
22942298 --------
@@ -2534,7 +2538,7 @@ def get_basic_selection(
25342538 out : NDBuffer | None = None ,
25352539 prototype : BufferPrototype | None = None ,
25362540 fields : Fields | None = None ,
2537- ) -> NDArrayLike :
2541+ ) -> NDArrayLikeOrScalar :
25382542 """Retrieve data for an item or region of the array.
25392543
25402544 Parameters
@@ -2552,8 +2556,8 @@ def get_basic_selection(
25522556
25532557 Returns
25542558 -------
2555- NDArrayLike
2556- An array-like containing the data for the requested region.
2559+ NDArrayLikeOrScalar
2560+ An array-like or scalar containing the data for the requested region.
25572561
25582562 Examples
25592563 --------
@@ -2754,7 +2758,7 @@ def get_orthogonal_selection(
27542758 out : NDBuffer | None = None ,
27552759 fields : Fields | None = None ,
27562760 prototype : BufferPrototype | None = None ,
2757- ) -> NDArrayLike :
2761+ ) -> NDArrayLikeOrScalar :
27582762 """Retrieve data by making a selection for each dimension of the array. For
27592763 example, if an array has 2 dimensions, allows selecting specific rows and/or
27602764 columns. The selection for each dimension can be either an integer (indexing a
@@ -2776,8 +2780,8 @@ def get_orthogonal_selection(
27762780
27772781 Returns
27782782 -------
2779- NDArrayLike
2780- An array-like containing the data for the requested selection.
2783+ NDArrayLikeOrScalar
2784+ An array-like or scalar containing the data for the requested selection.
27812785
27822786 Examples
27832787 --------
@@ -2990,7 +2994,7 @@ def get_mask_selection(
29902994 out : NDBuffer | None = None ,
29912995 fields : Fields | None = None ,
29922996 prototype : BufferPrototype | None = None ,
2993- ) -> NDArrayLike :
2997+ ) -> NDArrayLikeOrScalar :
29942998 """Retrieve a selection of individual items, by providing a Boolean array of the
29952999 same shape as the array against which the selection is being made, where True
29963000 values indicate a selected item.
@@ -3010,8 +3014,8 @@ def get_mask_selection(
30103014
30113015 Returns
30123016 -------
3013- NDArrayLike
3014- An array-like containing the data for the requested selection.
3017+ NDArrayLikeOrScalar
3018+ An array-like or scalar containing the data for the requested selection.
30153019
30163020 Examples
30173021 --------
@@ -3152,7 +3156,7 @@ def get_coordinate_selection(
31523156 out : NDBuffer | None = None ,
31533157 fields : Fields | None = None ,
31543158 prototype : BufferPrototype | None = None ,
3155- ) -> NDArrayLike :
3159+ ) -> NDArrayLikeOrScalar :
31563160 """Retrieve a selection of individual items, by providing the indices
31573161 (coordinates) for each selected item.
31583162
@@ -3170,8 +3174,8 @@ def get_coordinate_selection(
31703174
31713175 Returns
31723176 -------
3173- NDArrayLike
3174- An array-like containing the data for the requested coordinate selection.
3177+ NDArrayLikeOrScalar
3178+ An array-like or scalar containing the data for the requested coordinate selection.
31753179
31763180 Examples
31773181 --------
@@ -3340,7 +3344,7 @@ def get_block_selection(
33403344 out : NDBuffer | None = None ,
33413345 fields : Fields | None = None ,
33423346 prototype : BufferPrototype | None = None ,
3343- ) -> NDArrayLike :
3347+ ) -> NDArrayLikeOrScalar :
33443348 """Retrieve a selection of individual items, by providing the indices
33453349 (coordinates) for each selected item.
33463350
@@ -3358,8 +3362,8 @@ def get_block_selection(
33583362
33593363 Returns
33603364 -------
3361- NDArrayLike
3362- An array-like containing the data for the requested block selection.
3365+ NDArrayLikeOrScalar
3366+ An array-like or scalar containing the data for the requested block selection.
33633367
33643368 Examples
33653369 --------
0 commit comments