Working with attributes¶
Zarr arrays and groups support custom key/value attributes, which can be useful for storing application-specific metadata. For example:
import zarr
root = zarr.create_group(store="memory://attributes-demo")
root.attrs['foo'] = 'bar'
z = root.create_array(name='zzz', shape=(10000, 10000), dtype='int32')
z.attrs['baz'] = 42
z.attrs['qux'] = [1, 4, 7, 12]
print(sorted(root.attrs))
Attributes can be deleted with the del operator:
Note that each attribute assignment or deletion writes the node's metadata
document back to the store. To change several attributes in a single write,
use zarr.Array.update_attributes (or zarr.Group.update_attributes
for groups), which merges the given dict into the existing attributes and
returns the updated array or group:
Internally Zarr uses JSON to store array and group attributes, so attribute values must be JSON serializable.
When working with hierarchies that contain many arrays and groups, reading the attributes of each node separately can be slow. See Consolidated metadata for a way to store the metadata (including attributes) of all nodes in a hierarchy in a single document.