Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert python int into numpy.int64?

Tags:

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

like image 298
ShanZhengYang Avatar asked Oct 11 '17 23:10

ShanZhengYang


People also ask

Is Int64 int python?

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.

How large can an integer in python be in 64 bit?

64-bits ~ [-263, 263 – 1] = [ -9,223,372,036,854,775,808 , 9,223,372,036,854,775,807 ]

Does Python have int16?

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".


1 Answers

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.

like image 83
user2357112 supports Monica Avatar answered Oct 11 '22 16:10

user2357112 supports Monica