@@ -235,20 +235,25 @@ def __init__(self, zarr_array):
235235 self ._array = zarr_array
236236 self .shape = self ._array .shape
237237
238- # preserve vlen string object dtype (GH 7328)
238+ # preserve vlen string object dtype (GH 7328), but keep a native StringDType
239+ dtype = self ._array .dtype
239240 if (
240- self ._array .serializer
241+ dtype .kind != "T"
242+ and self ._array .serializer
241243 and self ._array .serializer .to_dict ()["name" ] == "vlen-utf8"
242244 ):
243245 dtype = coding .strings .create_vlen_dtype (str )
244- else :
245- dtype = self ._array .dtype
246246
247247 self .dtype = dtype
248248
249249 def get_array (self ):
250250 return self ._array
251251
252+ def _preserve_string_dtype (self , value ):
253+ if self .dtype .kind == "T" and isinstance (value , str ):
254+ return np .asarray (value , dtype = self .dtype )
255+ return value
256+
252257 def _oindex (self , key ):
253258 return self ._array .oindex [key ]
254259
@@ -288,13 +293,15 @@ def __getitem__(self, key):
288293 method = self ._vindex
289294 elif isinstance (key , indexing .OuterIndexer ):
290295 method = self ._oindex
291- return indexing .explicit_indexing_adapter (
296+ value = indexing .explicit_indexing_adapter (
292297 key , array .shape , indexing .IndexingSupport .VECTORIZED , method
293298 )
294299
295300 # if self.ndim == 0:
296301 # could possibly have a work-around for 0d data here
297302
303+ return self ._preserve_string_dtype (value )
304+
298305 async def async_getitem (self , key ):
299306 array = self ._array
300307 if isinstance (key , indexing .BasicIndexer ):
@@ -303,9 +310,10 @@ async def async_getitem(self, key):
303310 method = self ._async_vindex
304311 elif isinstance (key , indexing .OuterIndexer ):
305312 method = self ._async_oindex
306- return await indexing .async_explicit_indexing_adapter (
313+ value = await indexing .async_explicit_indexing_adapter (
307314 key , array .shape , indexing .IndexingSupport .VECTORIZED , method
308315 )
316+ return self ._preserve_string_dtype (value )
309317
310318
311319def _determine_zarr_chunks (enc_chunks , var_chunks , ndim , name ):
@@ -512,7 +520,9 @@ def extract_zarr_variable_encoding(
512520
513521# Function below is copied from conventions.encode_cf_variable.
514522# The only change is to raise an error for object dtypes.
515- def encode_zarr_variable (var , needs_copy = True , name = None ):
523+ def encode_zarr_variable (
524+ var , needs_copy = True , name = None , * , zarr_format : ZarrFormat | None = None
525+ ):
516526 """
517527 Converts a Variable into another Variable which follows some
518528 of the CF conventions:
@@ -535,6 +545,13 @@ def encode_zarr_variable(var, needs_copy=True, name=None):
535545 var = conventions .encode_cf_variable (var , name = name , coders = ZARR_CODERS )
536546 var = ensure_dtype_not_object (var , name = name )
537547
548+ if (
549+ zarr_format == 3
550+ and var .dtype .kind == "T"
551+ and var .dtype == np .dtypes .StringDType ()
552+ ):
553+ return var
554+
538555 # zarr allows unicode, but not variable-length strings, so it's both
539556 # simpler and more compact to always encode as UTF-8 explicitly.
540557 # TODO: allow toggling this explicitly via dtype in encoding.
@@ -854,17 +871,20 @@ def ds(self):
854871
855872 def open_store_variable (self , name ):
856873 zarr_array = self .members [name ]
857- data = indexing .LazilyIndexedArray (ZarrArrayWrapper (zarr_array ))
858874 try_nczarr = self ._mode == "r"
859875 dimensions , attributes = _get_zarr_dims_and_attrs (
860876 zarr_array , DIMENSION_KEY , try_nczarr
861877 )
862878 attributes = dict (attributes )
879+ array_wrapper = ZarrArrayWrapper (zarr_array )
880+ data = indexing .LazilyIndexedArray (array_wrapper )
863881
864882 encoding = {
865883 "chunks" : zarr_array .chunks ,
866884 "preferred_chunks" : dict (zip (dimensions , zarr_array .chunks , strict = True )),
867885 }
886+ if array_wrapper .dtype .kind == "T" :
887+ encoding ["dtype" ] = array_wrapper .dtype
868888
869889 encoding .update (
870890 {
@@ -930,7 +950,8 @@ def set_attributes(self, attributes):
930950 _put_attrs (self .zarr_group , attributes )
931951
932952 def encode_variable (self , variable , name = None ):
933- variable = encode_zarr_variable (variable , name = name )
953+ zarr_format = self .zarr_group .metadata .zarr_format
954+ variable = encode_zarr_variable (variable , name = name , zarr_format = zarr_format )
934955 return variable
935956
936957 def encode_attribute (self , a ):
0 commit comments