NumPy Structured Arrays
A structured dtype assigns names and types to fields within each record. It is useful for compact fixed-layout data, but it is not a relational database or a dataframe.
How it works
Field names select a typed column, while record indexing selects one structured element. Fixed-width Unicode fields can truncate longer text, so their length is an explicit storage limit. Structured arrays work well when the schema is stable and numerical interoperability is important. More flexible labelled table operations may be clearer in a dedicated table library, with its own dependencies and semantics.
Run the code
Select Try in playground to preload the complete code into the NumPy editor. Nothing executes on import; choose Run when ready. The first execution loads the local Pyodide runtime and NumPy package. Each ordinary run starts fresh, with no access to this page’s cookies or personal files. In-memory streams and temporary virtual files belong to the runtime, not your PC.
For local work, use a separate virtual environment and run python structured-arrays.py. Download the complete source. The reviewed local environment uses Python 3.12.14 and NumPy 2.5.4; the browser uses Pyodide 0.27.7 with NumPy 2.0.2. Compatible examples are checked in both. See the environment setup. No Apache or database server is required.
Example
import numpy as np
dtype = [("site", "U8"), ("litres", "f8")]
records = np.array([("roof-a", 12.), ("roof-b", 6.)], dtype=dtype)
print(records["site"].tolist())
print(float(records["litres"].sum()))
print(records.dtype.names)
Expected result
['roof-a', 'roof-b']
18.0
('site', 'litres')The named fields preserve one label and one floating measurement per record. The schema states a maximum text width, which must be checked before storing outside labels. It does not create key uniqueness or relationship constraints.
Use NumPy’s official reference for precise contracts, supported versions and numerical limitations.