Search code examples
pythonnumpyint

How to convert python int into numpy.int64?


Given a variable in python of type int, e.g.

z = 50
type(z) 
## outputs <class 'int'>

is there a straightforward way to convert this variable into numpy.int64?

It appears one would have to convert this variable into a numpy array, and then convert this into int64. That feels quite convoluted.

https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html


Solution

  • z_as_int64 = numpy.int64(z)
    

    It's that simple. Make sure you have a good reason, though - there are a few good reasons to do this, but most of the time, you can just use a regular int directly.