[Numpy-discussion] Checking if all array elements are whole numbers using the C-API

zoj613 blkzol001 at myuct.ac.za
Sat Jun 12 17:03:31 EDT 2021

Hi All,

Is there a C-API analogue for `np.asarray(a, dtype=int) != np.asarray(a)`
assuming that `a` is an array or python sequence object of whole numbers but
is assigned the float type (e.g a=array([1., 2., 3.]) or [1., 2., 3.])? I
tried `np.PyArray_FROM_OT(a, np.NPY_LONG) != np.PyArray_FROM_O(a)` but I get
the error message:

`TypeError: Cannot cast array data from dtype('float64') to dtype('int64')
according to the rule 'safe'`.

Is there a way I can achieve what I want using the numpy array c-api? I want
to check if an array/sequence has all elements as whole numbers so that the
check wont fail is an element is "2.0" instead of "2".

Sent from: http://numpy-discussion.10968.n7.nabble.com/

More information about the NumPy-Discussion mailing list