Every experiment ends up producing files, and the format you choose early on stays with the project for years. Python gives you many options, and four of them cover almost every situation in a lab: NumPy's own .npy and .npz files, HDF5, SQLite, and Zarr. This article compares them side by side and shows when each one is the right tool. Each format has its own in-depth tutorial in the Saving Data series.
Quick answer: use NumPy
.npyfor a single array you save and load whole. Use HDF5 for large arrays, many related datasets, and metadata in one file. Use SQLite for many small records you need to search. Use Zarr when data lives in the cloud or many processes write at once. For most labs, the best setup is SQLite for the metadata and HDF5 for the arrays.
Tested with Python 3.13, NumPy 2.4, h5py 3.16 and Zarr 3.4.
The comparison at a glance
NumPy .npy / .npz |
HDF5 (h5py) | SQLite | Zarr | |
|---|---|---|---|---|
| Best for | One array, saved and loaded whole | Large arrays, images, movies, many datasets in one file | Many small records: runs, samples, settings | Very large arrays, cloud storage, parallel writes |
| Read part of an array | Yes, .npy with mmap_mode |
Yes | No, a BLOB is read whole | Yes |
| Compression | .npz only (whole file) |
Yes, per chunk | No | Yes, per chunk |
| Metadata | No (only array names in .npz) |
Yes, attributes on any dataset or group | Yes, as table columns | Yes, attributes |
| Search and filter | No | No | Yes, with SQL | No |
| Append data | No | Yes, resizable datasets | Yes, insert rows | Yes, resize |
| Files on disk | One file | One file | One file | A folder with many files, or a zip |
| Readable from other languages | Mostly Python | Yes: MATLAB, C, Julia, IDL, ... | Yes, everywhere | Growing: Python, Julia, JavaScript, ... |
NumPy .npy and .npz: the simplest option
If your data is a single array and you always load all of it, nothing beats np.save:
import numpy as np
data = np.random.randn(100, 512, 512).astype(np.float32)
np.save('data.npy', data)
movie = np.load('data.npy', mmap_mode='r') # nothing is read yet
frame = movie[10] # only this frame is read from disk
The mmap_mode='r' option maps the file in memory, so you can read part of a very large array without loading the rest. With np.savez_compressed, you can also store several arrays in one compressed file, although you must load each array whole:
np.savez_compressed('data.npz', movie=data, exposure=np.array(0.01))
with np.load('data.npz') as f:
movie = f['movie']
The limitations show up as projects grow: there is no proper place for metadata, you cannot append to an existing file, and each new measurement creates more files to keep track of. The binary data article discusses these files, and pickle, in more detail.
HDF5: arrays and metadata in one file
HDF5 is a file that behaves like a folder structure. It holds as many datasets as you want, organized in groups, each with its own metadata. Datasets can be compressed, read in slices, and extended while you acquire:
import h5py
with h5py.File('data.h5', 'w') as f:
d = f.create_dataset('movie', data=data, chunks=(1, 512, 512), compression='gzip')
d.attrs['exposure_s'] = 0.01
with h5py.File('data.h5', 'r') as f:
frame = f['movie'][10]
It is the format behind many scientific instruments and facilities, and MATLAB's .mat files from version 7.3 are HDF5 files too. Its weak points are concurrency (one process writing at a time is the safe rule) and searching: HDF5 has no way of asking "which datasets were measured above 10 K?" without opening them one by one. The complete tutorial is How to use HDF5 files in Python.
SQLite: search through many records
SQLite is a full SQL database stored in a single file, and it is included with Python. It shines when you have many small records and you need to find some of them:
import sqlite3
conn = sqlite3.connect('runs.sqlite')
conn.execute('CREATE TABLE IF NOT EXISTS runs (id INTEGER PRIMARY KEY, sample TEXT, temperature REAL, file TEXT)')
with conn:
conn.execute('INSERT INTO runs (sample, temperature, file) VALUES (?, ?, ?)', ('A', 4.2, 'data.h5'))
cold_runs = conn.execute('SELECT file FROM runs WHERE temperature < 10').fetchall()
conn.close()
You can store arrays in SQLite as BLOBs, but every read loads and converts the whole array, and you lose the ability to work on the data with SQL. That is why "numpy vs sqlite" is usually the wrong question: they solve different problems. See Storing data with SQLite for the full tutorial.
Zarr: chunked arrays for the cloud and parallel work
Zarr stores N-dimensional arrays in chunks, like HDF5, but each chunk is a separate file (or object in cloud storage). This makes it a natural fit for data on S3 or similar services, and for many processes writing different chunks at the same time:
import zarr
z = zarr.create_array(store='data.zarr', shape=data.shape, chunks=(1, 512, 512), dtype='float32')
z[:] = data
z.attrs['exposure_s'] = 0.01
frame = zarr.open_array('data.zarr', mode='r')[10]
The interface is almost the same as h5py. The price is that a dataset becomes a folder with many small files, which is less convenient to copy around or attach to an email, and fewer tools outside the Python ecosystem can read it. For data acquired and analysed on a single lab computer, HDF5 is usually simpler. When data moves to a cluster or the cloud, or tools like Dask and xarray are part of the analysis, Zarr is worth a look.
The best of both: SQLite for metadata, HDF5 for arrays
In practice, the formats are not mutually exclusive. A setup that scales well for a lab is:
- One HDF5 file per measurement, or per day, holding the raw arrays and the instrument settings as attributes.
- One SQLite database with a row per measurement: date, sample, operator, key parameters, and the path to the HDF5 file.
You search with SQL to find the measurements you need, and open only those HDF5 files. A full example is in the SQLite article.
How to decide
- You save one array and load it whole:
.npy. - You save a few arrays together, and size matters:
.npzwithsavez_compressed. - You acquire images, spectra, or movies, possibly larger than memory, with settings attached: HDF5.
- You need to find measurements by their properties: SQLite, pointing to the data files.
- Your data lives in the cloud or is written by many processes in parallel: Zarr.
- You need to share files with colleagues using MATLAB or other languages: HDF5.
Whatever you choose, store the metadata together with the data from day one. Six months later, the exposure time or the sample name is exactly the information you will be missing.