Attributes

Attributes are a critical part of what makes HDF5 a “self-describing” format. They are small named pieces of data attached directly to Group and Dataset objects. This is the official way to store metadata in HDF5.

Each Group or Dataset has a small proxy object attached to it, at <obj>.attrs. Attributes have the following properties:

  • They may be created from any scalar or NumPy array

  • Each attribute should be small (generally < 64k)

  • There is no partial I/O (i.e. slicing); the entire attribute must be read.

The .attrs proxy objects are of class AttributeManager, below. This class supports a dictionary-style interface.

By default, attributes are iterated in alphanumeric order. However, if group or dataset is created with track_order=True, the attribute insertion order is remembered (tracked) in HDF5 file, and iteration uses that order. The latter is consistent with Python 3.7+ dictionaries.

The default track_order for all new groups and datasets can be specified globally with h5.get_config().track_order.

Reference

class h5py.AttributeManager(parent)

AttributeManager objects are created directly by h5py. You should access instances by group.attrs or dataset.attrs, not by manually creating them.

__iter__()

Get an iterator over attribute names.

__contains__(name)

Determine if attribute name is attached to this object.

__getitem__(name)

Retrieve an attribute.

__setitem__(name, val)

Create an attribute, overwriting any existing attribute. The type and shape of the attribute are determined automatically by h5py.

__delitem__(name)

Delete an attribute. KeyError if it doesn’t exist.

keys()

Get the names of all attributes attached to this object.

Returns

set-like object.

values()

Get the values of all attributes attached to this object.

Returns

collection or bag-like object.

items()

Get (name, value) tuples for all attributes attached to this object.

Returns

collection or set-like object.

get(name, default=None)

Retrieve name, or default if no such attribute exists.

get_id(name)

Get the low-level AttrID for the named attribute.

create(name, data, shape=None, dtype=None)

Create a new attribute, with control over the shape and type. Any existing attribute will be overwritten.

Parameters
  • name (String) – Name of the new attribute

  • data – Value of the attribute; will be put through numpy.array(data).

  • shape (Tuple) – Shape of the attribute. Overrides data.shape if both are given, in which case the total number of points must be unchanged.

  • dtype (NumPy dtype) – Data type for the attribute. Overrides data.dtype if both are given.

modify(name, value)

Change the value of an attribute while preserving its type and shape. Unlike AttributeManager.__setitem__(), if the attribute already exists, only its value will be changed. This can be useful for interacting with externally generated files, where the type and shape must not be altered.

If the attribute doesn’t exist, it will be created with a default shape and type.

Parameters
  • name (String) – Name of attribute to modify.

  • value – New value. Will be put through numpy.array(value).