@@ -313,13 +313,13 @@ def get_download_url(self):
313
313
"""
314
314
return api_utils .get_download_url (self .path , self .urlbase )
315
315
316
- def __getitem__ (self , key ):
316
+ def __getitem__ (self , item ):
317
317
"""
318
318
Retrieves a slice of the dataset.
319
319
320
320
Parameters
321
321
----------
322
- key : int, slice, tuple of ints and slices, or None
322
+ item : int, slice, tuple of ints and slices, or None
323
323
Specifies the slice to fetch.
324
324
325
325
Returns
@@ -340,7 +340,17 @@ def __getitem__(self, key):
340
340
>>> ds[0:10]
341
341
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
342
342
"""
343
- return self .slice (key , as_blosc2 = False )
343
+ if isinstance (item , str ): # used a filter or field to index so want blosc2 array as result
344
+ fields = np .dtype (eval (self .dtype )).fields
345
+ if fields is None :
346
+ raise ValueError ("The array is not structured (its dtype does not have fields)" )
347
+ if item in fields :
348
+ # A shortcut to access fields
349
+ return self .client .get_slice (self .path , as_blosc2 = True , field = item ) # arg key is None
350
+ else : # used a filter (possibly lazyexpr)
351
+ return self .client .get_slice (self .path , item , as_blosc2 = True )
352
+ else :
353
+ return self .slice (item , as_blosc2 = False )
344
354
345
355
def slice (
346
356
self , key : int | slice | Sequence [slice ], as_blosc2 : bool = True
@@ -871,24 +881,25 @@ def fetch(self, path, slice_=None):
871
881
[(1.0000500e-02, 1.0100005), (1.0050503e-02, 1.0100505)]],
872
882
dtype=[('a', '<f4'), ('b', '<f8')])
873
883
"""
874
- urlbase , path = _format_paths (self .urlbase , path )
875
- slice_ = api_utils .slice_to_string (slice_ ) # convert to string
876
- return api_utils .fetch_data (path , urlbase , {"slice_" : slice_ }, auth_cookie = self .cookie )
884
+ # Does the same as get_slice but forces return of np array
885
+ return self .get_slice (path , key = slice_ , as_blosc2 = False )
877
886
878
- def get_slice (self , path , key = None , as_blosc2 = True ):
887
+ def get_slice (self , path , key = None , as_blosc2 = True , field = None ):
879
888
"""Get a slice of a File/Dataset.
880
889
881
890
Parameters
882
891
----------
883
- key : int, slice, or sequence of slices
892
+ key : int, slice, sequence of slices or str
884
893
The slice to retrieve. If a single slice is provided, it will be
885
894
applied to the first dimension. If a sequence of slices is
886
895
provided, each slice will be applied to the corresponding
887
- dimension.
896
+ dimension. If str, is interpreted as filter.
888
897
as_blosc2 : bool
889
898
If True (default), the result will be returned as a Blosc2 object
890
899
(either a `SChunk` or `NDArray`). If False, it will be returned
891
900
as a NumPy array (equivalent to `self[key]`).
901
+ field: str
902
+ Shortcut to access a field in a structured array. If provided, `key` is ignored.
892
903
893
904
Returns
894
905
-------
@@ -905,12 +916,20 @@ def get_slice(self, path, key=None, as_blosc2=True):
905
916
dtype=[('a', '<f4'), ('b', '<f8')])
906
917
"""
907
918
urlbase , path = _format_paths (self .urlbase , path )
908
- # Convert slices to strings
909
- slice_ = api_utils .slice_to_string (key )
910
- # Fetch and return the data as a Blosc2 object / NumPy array
911
- return api_utils .fetch_data (
912
- path , urlbase , {"slice_" : slice_ }, auth_cookie = self .cookie , as_blosc2 = as_blosc2
913
- )
919
+ if field : # blosc2 doesn't support indexing of multiple fields
920
+ return api_utils .fetch_data (
921
+ path , urlbase , {"field" : field }, auth_cookie = self .cookie , as_blosc2 = as_blosc2
922
+ )
923
+ if isinstance (key , str ): # A filter has been passed
924
+ return api_utils .fetch_data (
925
+ path , urlbase , {"filter" : key }, auth_cookie = self .cookie , as_blosc2 = as_blosc2
926
+ )
927
+ else : # Convert slices to strings
928
+ slice_ = api_utils .slice_to_string (key )
929
+ # Fetch and return the data as a Blosc2 object / NumPy array
930
+ return api_utils .fetch_data (
931
+ path , urlbase , {"slice_" : slice_ }, auth_cookie = self .cookie , as_blosc2 = as_blosc2
932
+ )
914
933
915
934
def get_chunk (self , path , nchunk ):
916
935
"""
0 commit comments