@@ -409,4 +409,47 @@ We want to avoid a situation where the same native data type matches multiple Za
409
409
a NumPy data type should *uniquely * specify a single Zarr data type. But data type resolution is
410
410
dynamic, so it's not possible to statically guarantee this uniqueness constraint. Therefore, we
411
411
attempt data type resolution against *every * data type class, and if, for some reason, a native data
412
- type matches multiple Zarr data types, we treat this as an error and raise an exception.
412
+ type matches multiple Zarr data types, we treat this as an error and raise an exception.
413
+
414
+ If you have a NumPy data type and you want to get the corresponding ``ZDType `` instance, you can use
415
+ the ``parse_data_type `` function, which will use the dynamic resolution described above. ``parse_data_type ``
416
+ handles a range of input types:
417
+
418
+ - NumPy data types:
419
+
420
+ .. code-block :: python
421
+
422
+ >> > import numpy as np
423
+ >> > from zarr.dtype import parse_data_type
424
+ >> > my_dtype = np.dtype(' >M8[10s]' )
425
+ >> > parse_data_type(my_dtype, zarr_format = 2 )
426
+ DateTime64(endianness = ' big' , scale_factor = 10 , unit = ' s' )
427
+
428
+
429
+ - NumPy data type-compatible strings:
430
+
431
+ .. code-block :: python
432
+
433
+ >> > dtype_str = ' >M8[10s]'
434
+ >> > parse_data_type(dtype_str, zarr_format = 2 )
435
+ DateTime64(endianness = ' big' , scale_factor = 10 , unit = ' s' )
436
+
437
+ - ``ZDType `` instances:
438
+
439
+ .. code-block :: python
440
+
441
+ >> > from zarr.dtype import DateTime64
442
+ >> > zdt = DateTime64(endianness = ' big' , scale_factor = 10 , unit = ' s' )
443
+ >> > parse_data_type(zdt, zarr_format = 2 ) # Use a ZDType (this is a no-op)
444
+ DateTime64(endianness = ' big' , scale_factor = 10 , unit = ' s' )
445
+
446
+ - Python dictionaries (requires ``zarr_format=3 ``). These dictionaries must be consistent with the
447
+ ``JSON `` form of the data type:
448
+
449
+ .. code-block :: python
450
+
451
+ >> > dt_dict = {" name" : " numpy.datetime64" , " configuration" : {" unit" : " s" , " scale_factor" : 10 }}
452
+ >> > parse_data_type(dt_dict, zarr_format = 3 )
453
+ DateTime64(endianness = ' little' , scale_factor = 10 , unit = ' s' )
454
+ >> > parse_data_type(dt_dict, zarr_format = 3 ).to_json(zarr_format = 3 )
455
+ {' name' : ' numpy.datetime64' , ' configuration' : {' unit' : ' s' , ' scale_factor' : 10 }}
0 commit comments