Skip to content

ramonhagenaars/nptyping

Repository files navigation

PyPI version Downloads PyPI version codecov Code style

🧊 Type hints for NumPy
🐼 Type hints for pandas.DataFrame
💡 Extensive dynamic type checks for dtypes shapes and structures
🚀 Jump to the Quickstart

Example of a hinted numpy.ndarray:

>>> from nptyping import NDArray, Int, Shape

>>> arr: NDArray[Shape["2, 2"], Int]

Example of a hinted pandas.DataFrame:

>>> from nptyping import DataFrame, Structure as S

>>> df: DataFrame[S["name: Str, x: Float, y: Float"]]

Installation

Command Description
pip install nptyping Install the basics
pip install nptyping[pandas] Install with pandas extension
pip install nptyping[complete] Install with all extensions

Instance checking

Example of instance checking:

>>> import numpy as np

>>> isinstance(np.array([[1, 2], [3, 4]]), NDArray[Shape["2, 2"], Int])
True

>>> isinstance(np.array([[1., 2.], [3., 4.]]), NDArray[Shape["2, 2"], Int])
False

>>> isinstance(np.array([1, 2, 3, 4]), NDArray[Shape["2, 2"], Int])
False

nptyping also provides assert_isinstance. In contrast to assert isinstance(...), this won't cause IDEs or MyPy complaints. Here is an example:

>>> from nptyping import assert_isinstance

>>> assert_isinstance(np.array([1]), NDArray[Shape["1"], Int])
True

NumPy Structured arrays

You can also express structured arrays using nptyping.Structure:

>>> from nptyping import Structure

>>> Structure["name: Str, age: Int"]
Structure['age: Int, name: Str']

Here is an example to see it in action:

>>> from typing import Any
>>> import numpy as np
>>> from nptyping import NDArray, Structure

>>> arr = np.array([("Peter", 34)], dtype=[("name", "U10"), ("age", "i4")])
>>> isinstance(arr, NDArray[Any, Structure["name: Str, age: Int"]])
True

Subarrays can be expressed with a shape expression between square brackets:

>>> Structure["name: Int[3, 3]"]
Structure['name: Int[3, 3]']

NumPy Record arrays

The recarray is a specialization of a structured array. You can use RecArray to express them.

>>> from nptyping import RecArray

>>> arr = np.array([("Peter", 34)], dtype=[("name", "U10"), ("age", "i4")])
>>> rec_arr = arr.view(np.recarray)
>>> isinstance(rec_arr, RecArray[Any, Structure["name: Str, age: Int"]])
True

Pandas DataFrames

Pandas DataFrames can be expressed with Structure also. To make it more concise, you may want to alias Structure.

>>> from nptyping import DataFrame, Structure as S

>>> df: DataFrame[S["x: Float, y: Float"]]

More examples

Here is an example of a rich expression that can be done with nptyping:

def plan_route(
        locations: NDArray[Shape["[from, to], [x, y]"], Float]
) -> NDArray[Shape["* stops, [x, y]"], Float]:
    ...

More examples can be found in the documentation.

Documentation

  • User documentation
    The place to go if you are using this library.

  • Release notes
    To see what's new, check out the release notes.

  • Contributing
    If you're interested in developing along, find the guidelines here.

  • License
    If you want to check out how open source this library is.