Skip to content
Advertisement

In Python, how would you check if a number is one of the integer types?

In Python, how could you check if the type of a number is an integer without checking each integer type, i.e., 'int', 'numpy.int32', or 'numpy.int64'?

I thought to try if int(val) == val but this does not work when a float is set to an integer value (not type).

JavaScript

I want to filter out the last value, which is a numpy.float64.

Advertisement

Answer

You can use isinstance with a tuple argument containing the types of interest.

To capture all python and numpy integer types use:

JavaScript



Here is an example showing the results for several data types:

JavaScript

Result:

JavaScript


A second example which is true only for int and int64:

JavaScript

Result:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement