Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting for array-like

Tags:

python

numpy

What would be the correct type hint for a function that accepts an one dimensional array-like object? More specifically, my function uses np.percentile and I would like to 'match' np.percentile's flexibility in terms of the kind of array it accepts (List, pandas Series, numpy array, etc.). Below illustrates what I'm looking for:

def foo(arr: array-like) -> float:
    p = np.percentile(arr, 50)
    return p
like image 719
yippingAppa Avatar asked Jun 11 '26 19:06

yippingAppa


1 Answers

Use numpy.typing.ArrayLike:

from numpy.typing import ArrayLike

def foo(arr: ArrayLike) -> float:
    p = np.percentile(arr, 50)
    return p

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!