https://github.com/cheind/pydantic-numpy
Seamlessly integrate numpy arrays into pydantic models.
https://github.com/cheind/pydantic-numpy
Last synced: 10 months ago
JSON representation
Seamlessly integrate numpy arrays into pydantic models.
- Host: GitHub
- URL: https://github.com/cheind/pydantic-numpy
- Owner: cheind
- License: mit
- Created: 2022-03-25T04:15:58.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-12-09T11:37:55.000Z (over 3 years ago)
- Last Synced: 2025-04-14T00:17:04.985Z (about 1 year ago)
- Language: Python
- Size: 44.9 KB
- Stars: 59
- Watchers: 2
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/cheind/pydantic-numpy/actions/workflows/python-package.yml)
# pydantic-numpy
This library provides support for integrating numpy `np.ndarray`'s into pydantic models.
## Usage
For more examples see [test_ndarray.py](./tests/test_ndarray.py)
```python
from pydantic import BaseModel
import pydantic_numpy.dtype as pnd
from pydantic_numpy import NDArray, NDArrayFp32
class MyPydanticNumpyModel(BaseModel):
K: NDArray[pnd.float32]
C: NDArrayFp32 # <- Shorthand for same type as K
# Instantiate from array
cfg = MyPydanticNumpyModel(K=[1, 2])
# Instantiate from numpy file
cfg = MyPydanticNumpyModel(K={"path": "path_to/array.npy"})
# Instantiate from npz file with key
cfg = MyPydanticNumpyModel(K={"path": "path_to/array.npz", "key": "K"})
cfg.K
# np.ndarray[np.float32]
```
### Subfields
This package also comes with `pydantic_numpy.dtype`, which adds subtyping support such as `NDArray[pnd.float32]`. All subfields must be from this package as numpy dtypes have no [Pydantic support](https://pydantic-docs.helpmanual.io/usage/types/#generic-classes-as-types).
## Install
Via github
```shell
pip install git+https://github.com/cheind/pydantic-numpy.git
```
Via PyPi (note that the package might be outdated)
```shell
pip install pydantic-numpy
```
## History
The original idea originates from [this discussion](https://gist.github.com/danielhfrank/00e6b8556eed73fb4053450e602d2434), but stopped working for `numpy>=1.22`. This repository picks up where the previous discussion ended
- added designated repository for better handling of PRs
- added support for `numpy>1.22`
- Dtypes are no longer strings but `np.generics`. I.e. `NDArray['np.float32']` becomes `NDArray[np.float32]`
- added automated tests and continuous integration for different numpy/python versions