How do I find out if a numpy array contains integers?

后端 未结 5 1132
一个人的身影
一个人的身影 2021-02-03 18:46

I know there is a simple solution to this but can\'t seem to find it at the moment.

Given a numpy array, I need to know if the array contains integers.

Checking

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-03 19:17

    While the accepted answer from 2009 is still valid, there is a new and enhanced solution as of Numpy v0.19, released in September 2014:

    All numerical numpy types are now registered with the type hierarchy in the python numbers module.

    This allows for checking the dtype against Python's Numeric abstract base classes.

    isinstance(np.dtype('int8'), numbers.Integral)
    issubclass(np.dtype('int32').type, numbers.Integral)
    

    You can test against numbers.Complex, numbers.Real and numbers.Integral.

    P.S. As you don't need to access .type anymore you can shorten your line by a few characters now. ;)

提交回复
热议问题