35
35
from zarr .core .buffer import (
36
36
BufferPrototype ,
37
37
NDArrayLike ,
38
+ NDArrayLikeOrScalar ,
38
39
NDBuffer ,
39
40
default_buffer_prototype ,
40
41
)
@@ -1256,7 +1257,7 @@ async def _get_selection(
1256
1257
prototype : BufferPrototype ,
1257
1258
out : NDBuffer | None = None ,
1258
1259
fields : Fields | None = None ,
1259
- ) -> NDArrayLike :
1260
+ ) -> NDArrayLikeOrScalar :
1260
1261
# check fields are sensible
1261
1262
out_dtype = check_fields (fields , self .dtype )
1262
1263
@@ -1298,14 +1299,16 @@ async def _get_selection(
1298
1299
out_buffer ,
1299
1300
drop_axes = indexer .drop_axes ,
1300
1301
)
1302
+ if isinstance (indexer , BasicIndexer ) and indexer .shape == ():
1303
+ return out_buffer .as_scalar ()
1301
1304
return out_buffer .as_ndarray_like ()
1302
1305
1303
1306
async def getitem (
1304
1307
self ,
1305
1308
selection : BasicSelection ,
1306
1309
* ,
1307
1310
prototype : BufferPrototype | None = None ,
1308
- ) -> NDArrayLike :
1311
+ ) -> NDArrayLikeOrScalar :
1309
1312
"""
1310
1313
Asynchronous function that retrieves a subset of the array's data based on the provided selection.
1311
1314
@@ -1318,7 +1321,7 @@ async def getitem(
1318
1321
1319
1322
Returns
1320
1323
-------
1321
- NDArrayLike
1324
+ NDArrayLikeOrScalar
1322
1325
The retrieved subset of the array's data.
1323
1326
1324
1327
Examples
@@ -2268,14 +2271,15 @@ def __array__(
2268
2271
msg = "`copy=False` is not supported. This method always creates a copy."
2269
2272
raise ValueError (msg )
2270
2273
2271
- arr_np = self [...]
2274
+ arr = self [...]
2275
+ arr_np : NDArrayLike = np .array (arr , dtype = dtype )
2272
2276
2273
2277
if dtype is not None :
2274
2278
arr_np = arr_np .astype (dtype )
2275
2279
2276
2280
return arr_np
2277
2281
2278
- def __getitem__ (self , selection : Selection ) -> NDArrayLike :
2282
+ def __getitem__ (self , selection : Selection ) -> NDArrayLikeOrScalar :
2279
2283
"""Retrieve data for an item or region of the array.
2280
2284
2281
2285
Parameters
@@ -2286,8 +2290,8 @@ def __getitem__(self, selection: Selection) -> NDArrayLike:
2286
2290
2287
2291
Returns
2288
2292
-------
2289
- NDArrayLike
2290
- An array-like containing the data for the requested region.
2293
+ NDArrayLikeOrScalar
2294
+ An array-like or scalar containing the data for the requested region.
2291
2295
2292
2296
Examples
2293
2297
--------
@@ -2533,7 +2537,7 @@ def get_basic_selection(
2533
2537
out : NDBuffer | None = None ,
2534
2538
prototype : BufferPrototype | None = None ,
2535
2539
fields : Fields | None = None ,
2536
- ) -> NDArrayLike :
2540
+ ) -> NDArrayLikeOrScalar :
2537
2541
"""Retrieve data for an item or region of the array.
2538
2542
2539
2543
Parameters
@@ -2551,8 +2555,8 @@ def get_basic_selection(
2551
2555
2552
2556
Returns
2553
2557
-------
2554
- NDArrayLike
2555
- An array-like containing the data for the requested region.
2558
+ NDArrayLikeOrScalar
2559
+ An array-like or scalar containing the data for the requested region.
2556
2560
2557
2561
Examples
2558
2562
--------
@@ -2753,7 +2757,7 @@ def get_orthogonal_selection(
2753
2757
out : NDBuffer | None = None ,
2754
2758
fields : Fields | None = None ,
2755
2759
prototype : BufferPrototype | None = None ,
2756
- ) -> NDArrayLike :
2760
+ ) -> NDArrayLikeOrScalar :
2757
2761
"""Retrieve data by making a selection for each dimension of the array. For
2758
2762
example, if an array has 2 dimensions, allows selecting specific rows and/or
2759
2763
columns. The selection for each dimension can be either an integer (indexing a
@@ -2775,8 +2779,8 @@ def get_orthogonal_selection(
2775
2779
2776
2780
Returns
2777
2781
-------
2778
- NDArrayLike
2779
- An array-like containing the data for the requested selection.
2782
+ NDArrayLikeOrScalar
2783
+ An array-like or scalar containing the data for the requested selection.
2780
2784
2781
2785
Examples
2782
2786
--------
@@ -2989,7 +2993,7 @@ def get_mask_selection(
2989
2993
out : NDBuffer | None = None ,
2990
2994
fields : Fields | None = None ,
2991
2995
prototype : BufferPrototype | None = None ,
2992
- ) -> NDArrayLike :
2996
+ ) -> NDArrayLikeOrScalar :
2993
2997
"""Retrieve a selection of individual items, by providing a Boolean array of the
2994
2998
same shape as the array against which the selection is being made, where True
2995
2999
values indicate a selected item.
@@ -3009,8 +3013,8 @@ def get_mask_selection(
3009
3013
3010
3014
Returns
3011
3015
-------
3012
- NDArrayLike
3013
- An array-like containing the data for the requested selection.
3016
+ NDArrayLikeOrScalar
3017
+ An array-like or scalar containing the data for the requested selection.
3014
3018
3015
3019
Examples
3016
3020
--------
@@ -3151,7 +3155,7 @@ def get_coordinate_selection(
3151
3155
out : NDBuffer | None = None ,
3152
3156
fields : Fields | None = None ,
3153
3157
prototype : BufferPrototype | None = None ,
3154
- ) -> NDArrayLike :
3158
+ ) -> NDArrayLikeOrScalar :
3155
3159
"""Retrieve a selection of individual items, by providing the indices
3156
3160
(coordinates) for each selected item.
3157
3161
@@ -3169,8 +3173,8 @@ def get_coordinate_selection(
3169
3173
3170
3174
Returns
3171
3175
-------
3172
- NDArrayLike
3173
- An array-like containing the data for the requested coordinate selection.
3176
+ NDArrayLikeOrScalar
3177
+ An array-like or scalar containing the data for the requested coordinate selection.
3174
3178
3175
3179
Examples
3176
3180
--------
@@ -3339,7 +3343,7 @@ def get_block_selection(
3339
3343
out : NDBuffer | None = None ,
3340
3344
fields : Fields | None = None ,
3341
3345
prototype : BufferPrototype | None = None ,
3342
- ) -> NDArrayLike :
3346
+ ) -> NDArrayLikeOrScalar :
3343
3347
"""Retrieve a selection of individual items, by providing the indices
3344
3348
(coordinates) for each selected item.
3345
3349
@@ -3357,8 +3361,8 @@ def get_block_selection(
3357
3361
3358
3362
Returns
3359
3363
-------
3360
- NDArrayLike
3361
- An array-like containing the data for the requested block selection.
3364
+ NDArrayLikeOrScalar
3365
+ An array-like or scalar containing the data for the requested block selection.
3362
3366
3363
3367
Examples
3364
3368
--------
0 commit comments