.npy (single array) and .npz (multiple arrays, ZIP archive) binary formats. The API is split into two layers:
- Buffer functions — Work with
ArrayBuffer/Uint8Array/ strings. Imported fromnumpy-ts. - File I/O functions — Add file system I/O on top. Also imported from
numpy-ts. Works on Node.js, Bun, and Deno. Throws in browser environments.
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.
Returns:
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.
Returns:
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.
Returns:
NDArray — The parsed array.
serializeNpy
Serialize an NDArray to.npy format bytes.
Returns:
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.
NpzParseOptions:
Returns:
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.
NpzSerializeOptions:
Returns:
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).
Returns:
Record<string, NDArray> — Object mapping array names to NDArrays.
File I/O API
- Async
- Sync
load
Auto-detect file format (.npy or .npz) and load arrays from disk.Returns:
Promise<NDArray> for .npy files, Promise<NpzParseResult> for .npz files.save
Save a single NDArray to a.npy file.Returns:
Promise<void>loadNpy
Load a single.npy file from disk. Explicit alternative to the auto-detect load.Returns:
Promise<NDArray>saveNpy
Save a single NDArray to a.npy file. Explicit alternative to save.Returns:
Promise<void>loadNpzFile
Load a.npz file from disk and return parsed arrays.Returns:
Promise<NpzParseResult>saveNpzFile
Save multiple arrays to a.npz file on disk.Returns:
Promise<void>savez
Save multiple arrays to an uncompressed.npz file.Returns:
Promise<void>savez_compressed
Save multiple arrays to a DEFLATE-compressed.npz file.Returns:
Promise<void>