NumPy ndarray
NumPy ndarray Serialization Guide¶
Free-threaded wheels include NumPy
All wheels, GIL and free-threaded (cp314t), compile in the numpy integration. It
activates when NumPy is importable; without NumPy, safe_dump on a numpy.ndarray
raises YamlTypeError because the value is not a recognized type.
Complex numbers
YAML has no native complex type. Complex numbers are serialized as
(re+imj) strings. safe_load returns them as Python strings, not
complex objects.
Serialize NumPy arrays to YAML lists with zero-copy Rust processing.
Basic Usage¶
import numpy as np
import pyrs_yaml as y
# 1-D array
arr = np.array([1, 2, 3], dtype="int32")
yaml_str = y.safe_dump(arr)
# Output:
# - 1
# - 2
# - 3
# Round-trip back to Python list
data = y.safe_load(yaml_str)
assert data == [1, 2, 3]
Multi-dimensional Arrays¶
# 2-D matrix
matrix = np.array([[1.0, 2.0], [3.0, 4.0]], dtype="float64")
yaml_str = y.safe_dump(matrix)
data = y.safe_load(yaml_str)
assert data == [[1.0, 2.0], [3.0, 4.0]]
# 3-D cube
cube = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]], dtype="int64")
data = y.safe_load(y.safe_dump(cube))
assert data == [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
Supported dtypes¶
| NumPy dtype | YAML output | Example |
|---|---|---|
int8/16/32/64 |
Integer | 42 |
uint8/16/32/64 |
Integer | 42 |
float32/64 |
Float | 3.14 |
bool |
Boolean | true / false |
complex64/128 |
String | (1+2j) |
Special Values¶
# NaN
arr = np.array([1.0, float("nan"), 3.0])
data = y.safe_load(y.safe_dump(arr))
assert str(data[1]) == "nan"
# Infinity
arr = np.array([float("inf"), -float("inf")])
data = y.safe_load(y.safe_dump(arr))
assert data[0] == float("inf")
assert data[1] == float("-inf")
Negative Numbers¶
YAML 1.2 does not allow plain scalars starting with - in block sequences. Negative values are automatically quoted for correct round-trip:
arr = np.array([-100, 200], dtype="int16")
data = y.safe_load(y.safe_dump(arr))
assert data == [-100, 200] # round-trip correct
0-D Scalar Arrays¶
A 0-D array serializes as its scalar value — no wrapping list:
scalar = np.array(42, dtype="int32")
data = y.safe_load(y.safe_dump(scalar))
assert data == 42
0-D bool arrays
A 0-D bool array is recognised as a number before the array route ever sees
it, so it serializes as 1.0/0.0 (a float) rather than true/false. Use
a 1-element 1-D array (np.array([True])) for an explicit boolean round-trip.
Nested in Containers¶
NumPy arrays can be embedded in dicts or lists:
data = {"matrix": np.array([[1, 2], [3, 4]]), "label": "test"}
yaml_str = y.safe_dump(data)
loaded = y.safe_load(yaml_str)
assert loaded["matrix"] == [[1, 2], [3, 4]]
Unsupported Types¶
The following types raise YamlTypeError:
- String arrays
- Object arrays
- Structured arrays
- Non-numeric custom dtypes
Performance¶
- One
tobytes()snapshot per array, taken with the GIL held - Elements decoded in Rust with the GIL released
- Transposed, Fortran-ordered and strided arrays emit what they mean
- Arbitrary dimensions supported
See Also¶
- Serialization — Serialize plain Python objects
- NumPy Features — NumPy ndarray support overview
- NumPy API Reference — Module functions for serialization