24

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

ShanZhengYang
  • 14,842
  • 40
  • 116
  • 211
  • 2
    The real question is why would you need to do this? – Julien Oct 11 '17 at 23:56
  • 1
    @Julien It is a strange side-effect of certain numpy functions. e.g. `z = np.random.geometric(p=0.35, size=10000)` gives a different `type` than `z = np.random.geometric(p=0.35)` – ShanZhengYang Oct 12 '17 at 13:33

2 Answers2

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

user2357112
  • 235,058
  • 25
  • 372
  • 444
  • Nice! Just out of curiosity: is there a simple example (an outline would be enough) for some need of this (i don't doubt it exists)? – sascha Oct 12 '17 at 00:18
  • 3
    @sascha: Ensuring NumPy handling instead of regular Python handling for stuff like overflow, mostly. – user2357112 Oct 12 '17 at 01:24
  • 1
    @sascha Since you asked for examples, I will give you two, but in general any program where you want to use integers and need numbers greater than 2,147,483,647, you will need an int64. The first example is accounting. You only need simple ints when running the books for most things. Now imagine you want to balance the books for the entire nation (which has GDP's in the trillions). You cannot keep that number in an int32. I am working on identifying trees across a 10 million square kilometer area of Africa and want to uniquely id them. That number is more than a max int32. – EBo May 25 '18 at 09:33
  • 3
    @EBo: Python ints are arbitrary precision; they have *more* range than a `numpy.int64`. – user2357112 May 25 '18 at 16:42
  • @user2357112 You appear to be correct! It is explained and demonstrated in https://stackoverflow.com/questions/21031093/python-and-arbitrary-precision-integers I had never heard about a separate integer division '//'. It is a little different between python 2 and 3, so you need to check compatibility. Specifically, python2 type(9999999999999/3) is long, and python3 it is float. With the usage of '//' pyth2 returns a long, and python3 and in. Interestingly enough python does not appear to have a BigFloat or float128, but numpy does. Again thanks for teaching something new in python. – EBo May 28 '18 at 18:44
8
import numpy as np
z = 3
z = np.dtype('int64').type(z)
print(type(z))

outputs:

<class 'numpy.int64'>

But i support Juliens question in his comment.

sascha
  • 30,370
  • 6
  • 65
  • 104
  • I think this answer is much more generic than the below answer, and can be performed with variety of inputs from float to object. – Greg Nov 18 '21 at 07:27