Overview
NumPy 1.21, released on June 23, 2021, lays the groundwork for a new dtype infrastructure and improves type annotations.
Main Features
New dtype infrastructure
NumPy 1.21 introduces an internal dtype system overhaul that makes creating custom types easier. This infrastructure paves the way for future extensible data types.
python
import numpy as np
# Structured dtypes are better handled
dt = np.dtype([('x', np.float64), ('y', np.float64)])
points = np.array([(1.0, 2.0), (3.0, 4.0)], dtype=dt)
# Field access
print(points['x']) # [1. 3.]
print(points['y']) # [2. 4.]
# Type checking
print(np.issubdtype(dt, np.void)) # True
print(points.dtype.names) # ('x', 'y')
Improved type annotations
The .pyi stub files are enriched with more precise overloads for universal functions and array methods, improving the experience with mypy and IDEs.
python
import numpy as np
import numpy.typing as npt
# More precise annotations in 1.21
def distance(a: npt.NDArray[np.float64],
b: npt.NDArray[np.float64]) -> np.floating:
return np.linalg.norm(a - b)
p1 = np.array([1.0, 2.0, 3.0])
p2 = np.array([4.0, 5.0, 6.0])
print(f'Distance: {distance(p1, p2):.2f}') # 5.20
