30

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
  • 16,511
  • 49
  • 132
  • 234
  • 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

3 Answers3

47
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
  • 260,549
  • 28
  • 431
  • 505
  • 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
11
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
  • 32,238
  • 6
  • 68
  • 110
  • 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
0

For me converting to int64 was necessary for expressing a 16-digit account number as an integer without having any of its last for digits rounded off. I know using 16-digit account numbers as int instead of string is odd, but it's the system we inherited and so the account number is either int or string, depending on where in the code you happen to be in this massive system. Here's how I saw it in the existing code:

df_dataframe = df_dataframe.astype({'ACCOUNT_NUMBER': 'int64'})
Fatemeh Sangin
  • 558
  • 1
  • 4
  • 19
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33104901) – Zelemist Nov 10 '22 at 08:52