.npy (single array) and .npz (multiple arrays, ZIP archive) binary formats. The API is split into two layers:
- Browser functions — Work with
ArrayBuffer/Uint8Array/ strings. Imported fromnumpy-tsornumpy-ts/core. - Node.js functions — Add file system I/O on top. Imported from
numpy-ts/node.
Browser API
These functions work in any JavaScript environment (browser, Deno, Bun, Node.js) since they operate on bytes, not files.parseNpy
Parse a.npy buffer into an NDArray.
| Name | Type | Default | Description |
|---|---|---|---|
buffer | ArrayBuffer | Uint8Array | — | Raw bytes of a .npy file. |
NDArray — The parsed array with correct shape, dtype, and data.
parseNpyHeader
Parse only the header of a.npy file without reading the data. Useful for inspecting file metadata.
| Name | Type | Default | Description |
|---|---|---|---|
bytes | Uint8Array | — | Raw bytes of a .npy file. |
NpyHeader — Object with { descr, fortran_order, shape, version, headerLength }.
parseNpyData
Parse the data portion of a.npy file given pre-parsed metadata. Advanced use for custom streaming workflows.
| Name | Type | Default | Description |
|---|---|---|---|
bytes | Uint8Array | — | Raw data bytes (after the header). |
metadata | NpyMetadata | — | Metadata from header parsing. |
NDArray — The parsed array.
serializeNpy
Serialize an NDArray to.npy format bytes.
| Name | Type | Default | Description |
|---|---|---|---|
arr | NDArray | — | The array to serialize. |
Uint8Array — The .npy file as bytes, ready to be written or downloaded.
parseNpz / parseNpzSync
Parse a.npz buffer into a collection of named arrays. The async version supports DEFLATE-compressed files.
| Name | Type | Default | Description |
|---|---|---|---|
buffer | ArrayBuffer | Uint8Array | — | Raw bytes of a .npz file. |
options | NpzParseOptions | {} | Parse options. |
| Name | Type | Default | Description |
|---|---|---|---|
force | boolean | false | If true, skip arrays with unsupported dtypes instead of throwing. |
NpzParseResult — { arrays: Map<string, NDArray>, skipped: string[], errors: Map<string, string> }.
serializeNpz / serializeNpzSync
Serialize multiple arrays to.npz format. The async version supports DEFLATE compression.
| Name | Type | Default | Description |
|---|---|---|---|
arrays | NpzArraysInput | — | Arrays to serialize. Accepts NDArray[] (named arr_0, arr_1, …), Map<string, NDArray>, or Record<string, NDArray>. |
options | NpzSerializeOptions | {} | Serialization options. |
| Name | Type | Default | Description |
|---|---|---|---|
compress | boolean | false | Use DEFLATE compression (like np.savez_compressed). |
Uint8Array (or Promise<Uint8Array>) — The .npz file as bytes.
loadNpz / loadNpzSync
Convenience wrappers that parse an.npz buffer and return a simple Record<string, NDArray> (discarding error/skip metadata).
| Name | Type | Default | Description |
|---|---|---|---|
buffer | ArrayBuffer | Uint8Array | — | Raw bytes of a .npz file. |
options | NpzParseOptions | {} | Parse options. |
Record<string, NDArray> — Object mapping array names to NDArrays.
Node.js API
- Async
- Sync
load
Auto-detect file format (.npy or .npz) and load arrays from disk.| Name | Type | Default | Description |
|---|---|---|---|
path | string | — | Path to a .npy or .npz file. |
options | LoadOptions | undefined | Loading options. |
Promise<NDArray> for .npy files, Promise<NpzParseResult> for .npz files.save
Save a single NDArray to a.npy file.| Name | Type | Default | Description |
|---|---|---|---|
path | string | — | Output path (should end with .npy). |
arr | NDArray | — | Array to save. |
Promise<void>savez
Save multiple arrays to an uncompressed.npz file.| Name | Type | Default | Description |
|---|---|---|---|
path | string | — | Output path. .npz extension is appended if missing. |
arrays | Record<string, NDArray> | — | Named arrays to save. Also accepts NDArray[] or Map. |
Promise<void>savez_compressed
Save multiple arrays to a DEFLATE-compressed.npz file.| Name | Type | Default | Description |
|---|---|---|---|
path | string | — | Output path. .npz extension is appended if missing. |
arrays | Record<string, NDArray> | — | Named arrays to save. |
Promise<void>