NumPy Input and Output: lib.format() function

numpy.lib.format() function

Binary serialization

NPY format

A simple format for saving numpy arrays to disk with the full information about them.

The .npz format is the standard format for persisting multiple NumPy arrays on disk. A .npz file is a zip file containing multiple .npy files, one for each array.


  • Can represent all NumPy arrays including nested record arrays and object arrays.
  • Represents the data in its native binary form.
  • Supports Fortran-contiguous arrays directly.
  • Stores all of the necessary information to reconstruct the array including shape and dtype on a machine of a different architecture.
  • Is straightforward to reverse engineer. Datasets often live longer than the programs that created them.
  • Allows memory-mapping of the data. See open_memmep.
  • Can be read from a filelike stream object instead of an actual file.
  • Stores object arrays, i.e. arrays containing elements that are arbitrary Python objects.
    Files with object arrays are not to be mmapable, but can be read and written to disk.


  • Arbitrary subclasses of numpy.ndarray are not completely preserved. Subclasses will be accepted for writing, but only the array data will be written out.
    A regular numpy.ndarray object will be created upon reading the file.

File extensions

We recommend using the .npy and .npz extensions for files saved in this format.
This is by no means a requirement; applications may wish to use these file formats but use an extension specific to the application.
In the absence of an obvious alternative, however, we suggest using .npy and .npz.

Version numbering

The version numbering of these formats is independent of NumPy version numbering.
If the format is upgraded, the code in numpy.io will still be able to read and write Version 1.0 files.

Format Version 1.0

The first 6 bytes are a magic string: exactly \x93NUMPY.

The next 1 byte is an unsigned byte: the major version number of the file format, e.g. \x01.

The next 1 byte is an unsigned byte: the minor version number of the file format, e.g. \x00.
Note: the version of the file format is not tied to the version of the numpy package.

The next 2 bytes form a little-endian unsigned short int: the length of the header data HEADER_LEN.

The next HEADER_LEN bytes form the header data describing the array’s format. It is an ASCII string which contains a Python literal expression of a dictionary.
It is terminated by a newline (\n) and padded with spaces (\x20) to make the total of len(magic string) + 2 + len(length) + HEADER_LEN be evenly divisible by 64 for alignment purposes.

The dictionary contains three keys:



Version: 1.15.0


Name Description Required /
"descr" An object that can be passed as an argument to the numpy.dtype constructor to create the array’s dtype.
"fortran_order" Whether the array data is Fortran-contiguous or not. Since Fortran-contiguous arrays are a common form of non-C-contiguity,
we allow them to be written directly to disk for efficiency.
"shape" The shape of the array.
tuple of int

For repeatability and readability, the dictionary keys are sorted in alphabetic order.
This is for convenience only. A writer SHOULD implement this if possible. A reader MUST NOT depend on this.

Following the header comes the array data. If the dtype contains Python objects (i.e. dtype.hasobject is True), then the data is a Python pickle of the array. Otherwise the data is the contiguous (either C- or Fortran-, depending on fortran_order) bytes of the array.Consumers can figure out the number of bytes by multiplying the number of elements given by the shape (noting that shape=() means there is 1 element) by dtype.itemsize.

Format Version 2.0

The version 1.0 format only allowed the array header to have a total size of 65535 bytes. This can be exceeded by structured arrays with a large number of columns.
The version 2.0 format extends the header size to 4 GiB. numpy.save will automatically save in 2.0 format if the data requires it, else it will always use the more compatible 1.0 format.

The description of the fourth element of the header therefore has become:
"The next 4 bytes form a little-endian unsigned int: the length of the header data HEADER_LEN."


The .npy format, including motivation for creating it and a comparison of alternatives,
is described in the "npy-format" NEP, however details have evolved with time and this document is more current.

Python - NumPy Code Editor:

Previous: DataSource() function
Next: NumPy Home

Follow us on Facebook and Twitter for latest update.