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
You will often see the data type Int64 in Python which stands for 64 bit integer. The 64 refers to the memory allocated to store data in each cell which effectively relates to how many digits it can store in each “cell”. Allocating space ahead of time allows computers to optimize storage and processing efficiency.
64-bits ~ [-263, 263 – 1] = [ -9,223,372,036,854,775,808 , 9,223,372,036,854,775,807 ]
It is not possible in Python? It is waste of memory if the int object is small and int8/int16 cannot be specified. Native Python integers can be any size (limited by the amount of memory that's available) — so the short answer is "you can't".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With