2

As the title says, it is not about type of elements. I need to be sure that the values of elements are integers, i.e.

np.array([1, 2, 3])
np.array([1., 2.0, 9/3])

must both give [True, True, True] after the 'Are they integers?'-checking.

Is there a clean and pythonic/numpyic way of doing this?

I've tried some many-lines-combinations such as:

isinstance(x, (int, np.integer)) 
#or
(1.0).is_integer()

but they are cumbersome and ugly.

Grayrigel
  • 3,112
  • 5
  • 13
  • 25
user7647857
  • 359
  • 1
  • 9
  • I've made some progress in this regard. There is a standalone numpy function now available. Let's see if it makes it to numpy. – Mad Physicist Dec 31 '21 at 01:24

3 Answers3

3

What I use is this quantity % int(quantity) == 0

1

One More Way:

>>> x = np.array([1.,2.0,9/3])
>>> [not (i%1) for i in x]
[True, True, True]
0

Here is a oneliner using is_integer():

>>> x = np.array([1., 2.0, 9/3])
>>> [i.is_integer() for i in x]
[True, True, True]
Grayrigel
  • 3,112
  • 5
  • 13
  • 25