This page follows the format description in NumPy’s source,
numpy/lib/_format_impl.py
(the numpy.lib.format documentation), with type codes from the
array
interface and datetime pages and .npz details from
_npyio_impl.py, all read 2026-11-12. The format grew out of NumPy
Enhancement Proposal 1; the source says it is now more current than the NEP.
Layout
| Offset | Size | Field |
|---|---|---|
0 | 6 | Magic 93 4E 55 4D 50 59: the byte 93 and NUMPY. |
6 | 1 | Major version: 1, 2 or 3. |
7 | 1 | Minor version: 0. |
8 | 2 or 4 | Header length, little-endian: 2 bytes in version 1.0, 4 bytes in 2.0 and 3.0. |
10 or 12 | header length | The header text. |
| … | … | The array data. |
| Version | Difference |
|---|---|
| 1.0 | Header length is 2 bytes (at most 65,535); header text is ASCII (Latin-1). |
| 2.0 | Header length is 4 bytes, for headers too long for 1.0, such as structured types with many fields. |
| 3.0 | As 2.0, but the header text is UTF-8, so field names can use any characters. |
NumPy writes 1.0 when the header fits, then 2.0, then 3.0 if the header text needs characters outside Latin-1, and warns when it uses 2.0 or 3.0.
Header
The text of a Python dictionary literal, readable with
ast.literal_eval, with exactly these keys. Writers should sort the
keys alphabetically; readers must not depend on it.
| Key | Value |
|---|---|
'descr' | The element type: a type string (see type codes) or, for a structured array, a list of (name, type) or (name, type, shape) tuples. |
'fortran_order' | True: data is stored column-major (first index fastest). False: row-major (C order). |
'shape' | A tuple of dimensions, such as (3, 4); () for a single value; (5,) for one dimension. |
The header is padded with spaces and ends with a line feed, so that the total of magic, versions, length field and header is a multiple of 64 bytes (16 in files from older writers, see the Alignment item in the structured arrays and checks list). The data then starts aligned, which allows memory-mapping. NumPy also adds spare spaces after the dictionary so the length of the first (or, in Fortran order, last) dimension can be rewritten in place when an array grows.
93 4E 55 4D 50 59 01 00 76 00
{'descr': '<f8', 'fortran_order': False, 'shape': (3, 4), } …spaces… \n
Here the header is 0x76 = 118 bytes, so the data starts at byte 128.
Type codes
A type string is a byte-order character, a kind character and a size in bytes:
<f8 is a little-endian 8-byte float.
| Character | Meaning |
|---|---|
<, > | Little-endian, big-endian. |
| | Byte order is not relevant. |
b | Boolean, 1 byte per element. |
i, u | Signed and unsigned integers. |
f | Floating point. |
c | Complex floating point. |
m, M | Timedelta and datetime, with a unit: <M8[ns], <m8[s]. Units: Y, M, W, D, h, m, s, ms, us, ns, ps, fs, as. |
S | A fixed-length sequence of bytes (char): |S10. |
U | Unicode text of fixed length, in UCS-4 characters: <U10 is 40 bytes. |
V | Raw bytes (void): |V16. |
O | Python objects: the data is a pickle of the array, not raw values. NumPy refuses to load it unless allow_pickle=True, because a pickle can run code. |
Structured types, datetimes and limits
From _format_impl.py (dtype_to_descr, descr_to_dtype
and _read_array_header, read 2026-11-13) and the array interface page.
- Plain types. A non-structured array has
'descr'equal to its type string. The dtype's owndescr([('', '<f4')]) is not used because the constructor would read the empty name as a request for a default name. - Structured arrays.
'descr'is a list of(name, type)or(name, type, shape)tuples, in memory order. The type part is a type string or another list (a nested structured type); a shape tuple makes that field a sub-array repeated that many times. A name may itself be a tuple(title, name). The bytes of each element are the fields one after another, with the offsets given by adding up the sizes. - Padding. A field named
''whose type is a void (|Vn) with no sub-fields is padding: the reader skips its bytes and creates no field. - Empty field names. NumPy's own documentation warns that a field whose name is empty is given the name
f0,f1, ... when loaded; the data is intact, only the names differ, andloaded.view(correct_dtype)restores the right dtype. This needs no change of file format. - Datetime and timedelta.
M8andm8take a unit in brackets (Y M W D h m s ms us ns ps fs as); without brackets the unit is generic, which NumPy 2.5 deprecates (generic units will raise an error), andNaTin datetime64 will require an explicit unit. Stored values are 64-bit integers counted from the epoch 1970-01-01T00:00. - Custom dtypes not in NumPy's public set are written as
|Owith the dtype pickled along with the data, so loading needsallow_pickle=True. Metadata attached to a dtype is dropped on writing, with a warning. - Size limits. Version 1.0 stores the header length in 2 bytes, so at most 65,535; version 2.0 and 3.0 use 4 bytes ("extends the header size to 4 GiB"). The writer tries 1.0, then 2.0 (a writer that finds the header contains characters outside Latin-1 moves to 3.0), warning that 2.0 needs NumPy 1.9 or later and 3.0 needs 1.17 or later.
- Reader limit. By default NumPy refuses a header longer than
_MAX_HEADER_SIZE= 10,000 characters, becauseast.literal_evalis not safe on large inputs; the check can be raised withmax_header_size, and is ignored whenallow_pickle=True. - Header checks. The header must be a dictionary with exactly the keys
descr,fortran_orderandshape;shapea tuple of integers andfortran_ordera bool. A header written by Python 2 (versions 1.0 and 2.0 only) that fails to parse is retried after filtering, with a warning. - Alignment. The data offset is a multiple of 64 bytes (
ARRAY_ALIGN) in the current source (_format_impl.py); the writer pads the header with spaces and ends it with\n. Older writers aligned to 16: NEP 1's version 1.0 text pads so that magic string + 4 + HEADER_LEN is a multiple of 16, so files from those writers have a data offset that is a multiple of 16 but not necessarily of 64. A reader must use HEADER_LEN, never assume 64. - Payload size. After the header (and for a non-object dtype) the data is the contiguous bytes of the array, C order or Fortran order by
fortran_order. Its size is the number of elements, the product of theshapeentries (shape=()means 1 element), timesdtype.itemsize. - Subclasses are not preserved. A subclass of
numpy.ndarrayis accepted for writing but only the array data is written; reading creates a plainnumpy.ndarray. Building a format for a particular subclass is outside the general NPY format (NEP 1).
Object arrays: the pickle after the header
From _format_impl.py (write_array, read_array) and
numpy/_core/src/multiarray/methods.c (array_reduce,
array_reduce_ex, array_setstate), main branch, read 2026-11-13.
- When. If the dtype contains Python objects (
dtype.hasobject), or is a user-defined dtype that is not a legacy one, the writer does not write raw bytes: it callspickle.dump(array, fp, protocol=4)(with any extrapickle_kwargs, which cannot override those), so the data after the header is an ordinary pickle stream of the whole array, starting with the protocol 4 header80 04(see the pickle pages for the opcodes). Withallow_pickle=Falsewriting fails with “Object arrays cannot be saved when allow_pickle=False” (or the same for user-defined dtypes). The header still carries the shape and the object dtype'|O'(for a user-defined dtype the writer lies that the dtype is object). The reader unpickles withpickle.loadonly ifallow_pickle=True, otherwise raises an error; a Python 2 file can need anencoding=option. - What the pickle contains. An ndarray is pickled through
__reduce_ex__. For protocols below 5, for dtypes with objects, for subclasses and for zero-size items it usesarray_reduce, the generic route: a 3-tuple of the callablenumpy._core._multiarray_umath._reconstruct, the arguments(type(array), (0,), b'b')(the array class, a dummy shape and a dummy dtype code) and a state tuple of 5 items: (1) the integer pickle version, 1; (2) the shape tuple; (3) the dtype object, with the byte order set; (4) a bool, true if the array is Fortran-ordered; (5) the data, which is a bytes string for ordinary dtypes, or, for dtypes carrying the list-pickle flag (NPY_LIST_PICKLE, which the source comment describes as “a string, or a list or any user-defined object”), a Python list of the elements. Reading goes through__setstate__, which accepts version 1 and the older version 0 (a 4-tuple without the version) and rejects other versions; for a list it checks that the number of elements equals the product of the shape. With pickle protocol 5 and a contiguous plain buffer NumPy instead uses aPickleBufferroute, but the NPY writer always uses protocol 4. - Security. A pickle can run code when loaded, which is why
allow_pickledefaults to false on reading.
Datetime unit multiples and divisors
From numpy/_core/src/multiarray/datetime.c (parse_datetime_extended_unit_from_string,
parse_datetime_metadata_from_metastr, convert_datetime_divisor_to_multiple,
parse_dtype_from_datetime_typestr), main branch, read 2026-11-13.
- Syntax. The type string is
M8orm8(also accepted:datetime64,timedelta64) followed by an optional metadata string in brackets:[, an optional positive integer multiplier (at most the largest 32-bit integer), the unit code, an optional/and an integer divisor, then]. An empty metadata string means generic units; the multiplier defaults to 1. So<M8[10s]counts in units of ten seconds: a stored value 3 is 30 seconds after the epoch, and the stored values are still 64-bit integers. - Divisors. A divisor is turned into a multiple of a finer unit: the code tries each candidate factor of the unit in order and uses the first that the divisor divides evenly, switching to the corresponding finer unit and multiplying the multiplier by the quotient (so
[s/10], with the factors 1000 (ms) and 1000000 (us) tried, becomes 100 ms). The tables of candidate factors are Y: 12 (M), 52 (W), 365 (D); M: 4 (W), 30 (D), 720 (h); W: 7 (D), 168 (h), 10080 (m); D: 24 (h), 1440 (m), 86400 (s); h: 60 (m), 3600 (s); m: 60 (s), 60000 (ms); and for s and every finer unit up to ps: 1000 and 1000000 of the next two finer units (fs has only 1000 of as). A divisor that divides none of them is an error (“divisor is not a multiple of a lower-unit”), so[s/4]is 250 ms and[h/12]is 5 minutes, but[s/3]and[s/7]are refused; generic units cannot take a divisor, and attoseconds cannot take one at all. Weeks have a quirk: the loop for W runs four times over a three-entry row, and the empty fourth entry divides anything, so a divisor of W that fits none of 7, 168 and 10080 ([W/11]) does not fail but gives a multiplier of 0 in years (re-read 2026-11-19). The metadata also accepts the unit wordgeneric, and a stored value of −263 is NaT. - Epoch and span. Datetimes always count from 1970-01-01T00:00; the time span is the range of a 64-bit integer times the unit. From the NumPy datetime documentation (arrays.datetime, read 2026-11-13):
Yyear ±9.2e18 years,Mmonth ±7.6e17 years,Wweek ±1.7e17 years,Dday ±2.5e16 years,hhour ±1.0e15 years,mminute ±1.7e13 years,ssecond ±2.9e11 years,msmillisecond ±2.9e8 years,us(orµs) microsecond ±2.9e5 years (290301 BC to 294241 AD),nsnanosecond ±292 years (1678 AD to 2262 AD),pspicosecond ±106 days,fsfemtosecond ±2.6 hours,asattosecond ±9.2 seconds (the last three all within 1969 to 1970 AD). A multiplier scales the span by the same factor. The span ofWis exactly 7 times that ofDandDis exactly 24 timesh.
Why the format exists (NEP 1). NEP 1 proposes NPY as a standard binary file for persisting a single array, storing all shape and dtype information needed to rebuild it even on another machine architecture, as simple as possible, in pure Python distributed with NumPy. Pickle was rejected as the answer because dumping or loading needs a duplicate of the data in memory and the data cannot be memory-mapped; tofile()/fromfile() because they store no shape or dtype and cannot handle object arrays. It deliberately does not aim at what HDF5 does. The version 1.0 implementation first appeared in NumPy 1.0.5 and version 2.0 in 1.9.0; the document is in the public domain.
.npz archives
numpy.savez writes several arrays into one ZIP
file, each as a .npy member named after its keyword (or
arr_0, arr_1…). savez_compressed uses Deflate;
savez stores them uncompressed.