Miscellaneous: HDF5, YAML, ASDF, pickle (astropy.io.misc)

The astropy.io.misc module contains miscellaneous input/output routines that do not fit elsewhere, and are often used by other Astropy sub-packages. For example, astropy.io.misc.hdf5 contains functions to read/write Table objects from/to HDF5 files, but these should not be imported directly by users. Instead, users can access this functionality via the Table class itself (see Unified file read/write interface). Routines that are intended to be used directly by users are listed in the astropy.io.misc section.

astropy.io.misc Package

This package contains miscellaneous utility functions for data input/output with astropy.


fnpickle(object, fileorname[, usecPickle, …]) Pickle an object to a specified file.
fnunpickle(fileorname[, number, usecPickle]) Unpickle pickled objects from a specified file and return the contents.

astropy.io.misc.hdf5 Module

This package contains functions for reading and writing HDF5 tables that are not meant to be used directly, but instead are available as readers/writers in astropy.table. See Unified file read/write interface for more details.


read_table_hdf5(input[, path, …]) Read a Table object from an HDF5 file
write_table_hdf5(table, output[, path, …]) Write a Table object to an HDF5 file

astropy.io.misc.yaml Module

This module contains functions for serializing core astropy objects via the YAML protocol.

It provides functions dump, load, and load_all which call the corresponding functions in PyYaml but use the AstropyDumper and AstropyLoader classes to define custom YAML tags for the following astropy classes:


This module requires PyYaml version 3.12 or later.


>>> from astropy.io.misc import yaml
>>> import astropy.units as u
>>> from astropy.time import Time
>>> from astropy.coordinates import EarthLocation

>>> t = Time(2457389.0, format='mjd',
...          location=EarthLocation(1000, 2000, 3000, unit=u.km))
>>> td = yaml.dump(t)

>>> print(td)
format: mjd
in_subfmt: '*'
jd1: 4857390.0
jd2: -0.5
location: !astropy.coordinates.earth.EarthLocation
  ellipsoid: WGS84
  x: !astropy.units.Quantity
    unit: &id001 !astropy.units.Unit {unit: km}
    value: 1000.0
  y: !astropy.units.Quantity
    unit: *id001
    value: 2000.0
  z: !astropy.units.Quantity
    unit: *id001
    value: 3000.0
out_subfmt: '*'
precision: 3
scale: utc

>>> ty = yaml.load(td)
>>> ty
<Time object: scale='utc' format='mjd' value=2457389.0>

>>> ty.location  
<EarthLocation (1000., 2000., 3000.) km>


load(stream) Parse the first YAML document in a stream using the AstropyLoader and produce the corresponding Python object.
load_all(stream) Parse the all YAML documents in a stream using the AstropyLoader class and produce the corresponding Python object.
dump(data[, stream]) Serialize a Python object into a YAML stream using the AstropyDumper class.


AstropyLoader(stream) Custom SafeLoader that constructs astropy core objects as well as Python tuple and unicode objects.
AstropyDumper(stream[, default_style, …]) Custom SafeDumper that represents astropy core objects as well as Python tuple and unicode objects.

Class Inheritance Diagram

Inheritance diagram of astropy.io.misc.yaml.AstropyLoader, astropy.io.misc.yaml.AstropyDumper

astropy.io.misc.asdf Package

The asdf subpackage contains code that is used to serialize astropy types so that they can be represented and stored using the Advanced Scientific Data Format (ASDF). This subpackage defines classes, referred to as tags, that implement the logic for serialization and deserialization.

ASDF makes use of abstract data type definitions called schemas. The tag classes provided here are specific implementations of particular schemas. Some of the tags in Astropy (e.g., those related to transforms) implement schemas that are defined by the ASDF Standard. In other cases, both the tags and schemas are defined within Astropy (e.g., those related to many of the coordinate frames).

Astropy currently has no ability to read or write ASDF files itself. In order to process ASDF files it is necessary to make use of the standalone asdf package. Users should never need to refer to tag implementations directly. Their presence should be entirely transparent when processing ASDF files.

If both asdf and astropy are installed, no further configuration is required in order to process ASDF files. The asdf package has been designed to automatically detect the presence of the tags defined by astropy.

Documentation on the ASDF Standard can be found here. Documentation on the ASDF Python module can be found here.