Introduction
An .npy file holds one array saved by NumPy,
the array library for Python, with numpy.save. It stores the array’s
shape, element type and byte order in a short text header, then the raw values, so it
loads quickly and can be memory-mapped without reading the whole file.
An .npz file holds several arrays: it is a
ZIP archive of .npy files, one per array,
written by numpy.savez or, compressed, by
numpy.savez_compressed.
Structure
The file starts with the magic string \x93NUMPY (the byte
93 and the letters NUMPY), then two bytes for the major and
minor version of the format, then the length of the header: 2 bytes, little-endian, in
version 1.0, and 4 bytes in versions 2.0 and 3.0.
The header is a Python dictionary written as text, for example
{'descr': '<f8', 'fortran_order': False, 'shape': (3, 4), }. The
descr gives the element type and byte order, fortran_order
says whether the array is stored column by column, and shape gives its
dimensions. The header is padded with spaces and ends with a line feed, so that the
data starts on a multiple of 64 bytes (16 in files from older writers). The array’s values
follow with nothing after them: their size is the product of the shape entries (1 for
()) times the item size.
0Magic\x93NUMPY6 bytes6Version major and minor2 bytes8Header length2 or 4 bytes…Header descr, fortran_order and shape as text, paddedvaries…Array data the raw valuesvaries
The header and data types page gives every version, header key and type code.
Not described
The reference follows NumPy's _format_impl.py, methods.c and
datetime.c, NEP 1, and the array interface and datetime documents, and
now states the payload size rule, that subclasses are not preserved, and the 16-byte
alignment of older files. One small item is not written out: the type-string page
does not list the valid sizes per kind (for example f2, f4,
f8, c8, c16) or the array-interface kind
t (bit field).
Known, not yet covered
- Viewer: the explorer opens an
.npyfile and shows its structure down to every byte: the magic, the version, the header length, the header's dictionary entry by entry (read as Python'sast.literal_evalreads it, checked against Python itself on 83 header strings), then the array data element by element, a structured element field by field and a sub-array item by item. The Values table lists every element in pages, with its index (C or Fortran order), for every type the reference lists: booleans, 1 to 8 byte integers, half, single and double floats, complex, long double (x87 and binary128, said to be a guess where the file cannot say), datetime and timedelta in every unit (as dates), byte strings, UCS-4 strings and void, in both byte orders, and structured types with nested fields, titles, empty names, padding and sub-arrays. It checks the rules (the keys, the shape and type, the 10,000 character limit, the alignment, the data length, bytes after the array) and says which of them NumPy itself refuses. The statistics (minimum, maximum, mean, nan and inf counts) read the first 8,000,000 elements of each field and say so. An array of Python objects is a pickle after the header: it is offered as a file of its own for the pickle viewer. The.npzarchive opens as a ZIP, and each.npymember opens here. Datetime and timedelta types with a multiplier or a divisor in their unit are read asdatetime.creads them:M8[25s]shows each value as the date it means with the stored count kept (1970-01-01T00:00:50 (2 × 25 s)),m8[2ms]as the duration (10 milliseconds (5 × 2 ms)), NaT as NaT, and a divisor is turned into a multiple of a smaller unit as NumPy does (M8[s/4]is steps of 250 ms, and the type's note says so); a divisor NumPy refuses (M8[s/3]) is reported as an invalid type. The statistics tab says in its title and in its Elements read column that only the first 8,000,000 elements are read. Not covered: a\N{name}escape inside a header string (no Unicode name table), and NumPy itself is not installed, so the values are checked against hand-written layouts and NumPy's own test files, not against NumPy's output.
Links
Software
- Python – with NumPy installed, loads and saves these files (Windows, macOS, Linux)
Developers
- numpy.load – with
numpy.save, reads and writes .npy and .npz (Python) - cnpy – reads and writes .npy and .npz files (C++)
Reference
- numpy.lib.format – the NumPy documentation of the format