Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hint for NumPy ndarray dtype?

I would like a function to include a type hint for NumPy ndarray's alongside with its dtype.

With lists, for example, one could do the following...

def foo(bar: List[int]):
   ...

...in order to give a type hint that bar has to be list consisting of int's.

Unfortunately, this syntax throws exceptions for NumPy ndarray:

def foo(bar: np.ndarray[np.bool]):
   ...

> np.ndarray[np.bool]) (...) TypeError: 'type' object is not subscriptable

Is it possible to give dtype-specific type hints for np.ndarray?

like image 636
daniel451 Avatar asked Feb 03 '19 14:02

daniel451


People also ask

What is Dtype of Ndarray?

Every ndarray has an associated data type (dtype) object. This data type object (dtype) informs us about the layout of the array. This means it gives us information about: Type of the data (integer, float, Python object, etc.) Size of the data (number of bytes)

What is Ndarray type in Python?

Numpy with Python The most important object defined in NumPy is an N-dimensional array type called ndarray.

Is Ndarray same as Numpy array?

numpy. array is just a convenience function to create an ndarray ; it is not a class itself. You can also create an array using numpy. ndarray , but it is not the recommended way.

Can Numpy Ndarray hold any type of object?

Yes, a numpy array can store different data String, Integer, Complex, Float, Boolean.

Is it possible to specify dtype in NumPy array type hints?

To the best of my knowledge it's not possible yet to specify dtype in numpy array type hints in function signatures. It is planned to be implemented at some point in the future.

What is dtypelike in NumPy?

The DTypeLike type tries to avoid creation of dtype objects using dictionary of fields like below: Although this is valid NumPy code, the type checker will complain about it, since its usage is discouraged. Please see : Data type objects

What is ndarray in NumPy?

Numpy 1.21 includes a numpy.typing module with an NDArray generic type. A generic version of np.ndarray [Any, np.dtype [+ScalarType]]. Can be used during runtime for typing arrays with a given dtype and unspecified shape. As of 11/10/2021, support for shapes is still a work in progress per numpy/numpy#16544.

How to create a data type object in NumPy?

1. Constructing a data type (dtype) object: A data type object is an instance of the NumPy.dtype class and it can be created using NumPy.dtype. obj: Object to be converted to a data-type object.


2 Answers

You could check out nptyping:

from nptyping import NDArray, Bool  def foo(bar: NDArray[Bool]):    ... 

Or you could just use strings for type hints:

def foo(bar: 'np.ndarray[np.bool]'):    ... 
like image 160
R H Avatar answered Sep 21 '22 15:09

R H


Check out data-science-types package.

pip install data-science-types

MyPy now has access to Numpy, Pandas, and Matplotlib stubs. Allows scenarios like:

# program.py

import numpy as np
import pandas as pd

arr1: np.ndarray[np.int64] = np.array([3, 7, 39, -3])  # OK
arr2: np.ndarray[np.int32] = np.array([3, 7, 39, -3])  # Type error

df: pd.DataFrame = pd.DataFrame({'col1': [1,2,3], 'col2': [4,5,6]}) # OK
df1: pd.DataFrame = pd.Series([1,2,3]) # error: Incompatible types in assignment (expression has type "Series[int]", variable has type "DataFrame")

Use mypy like normal.

$ mypy program.py

Usage with function-parameters

def f(df: pd.DataFrame):
    return df.head()

if __name__ == "__main__":
    x = pd.DataFrame({'col1': [1, 2, 3, 4, 5, 6]})
    print(f(x))

$ mypy program.py
> Success: no issues found in 1 source file
like image 23
kevin_theinfinityfund Avatar answered Sep 20 '22 15:09

kevin_theinfinityfund