How do I convert a float NumPy array into an int NumPy array?
Asked
Active
Viewed 4.6e+01k times
4 Answers
476
Use the astype
method.
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> x.astype(int)
array([[1, 2],
[1, 2]])

Mateen Ulhaq
- 24,552
- 19
- 101
- 135

BrenBarn
- 242,874
- 37
- 412
- 384
-
35Just make sure you don't have `np.inf`or `np.nan` in your array, since they have surprising results. For example, `np.array([np.inf]).astype(int)` outputs `array([-9223372036854775808])`. – Garrett Jan 22 '15 at 08:42
-
On my machine, `np.array([np.inf]).astype(int)`, `np.array([-np.inf]).astype(int)`, and `np.array([np.nan]).astype(int)` all return the same thing. Why? – BallpointBen May 14 '18 at 20:47
-
1@BallpointBen: `nan` and `inf` are floating-point values and can't be meaningfully converted to int. As the comment before yours notes, there will be surprising behavior, and I don't think the precise behavior is well-defined. If you want to map `nan` and `inf` to certain values, you need to do that yourself. – BrenBarn May 15 '18 at 18:21
-
1Note that x.astype(int)[0][0] is not of type `int`. It's `numpy.int32`. – chris Jun 06 '18 at 19:34
-
1Note that although this does convert the array to ints, @fhtuft's answer that may result in less surprises – Nathan Musoke Apr 15 '20 at 18:07
78
Some numpy functions for how to control the rounding: rint, floor,trunc, ceil. depending how u wish to round the floats, up, down, or to the nearest int.
>>> x = np.array([[1.0,2.3],[1.3,2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> y = np.trunc(x)
>>> y
array([[ 1., 2.],
[ 1., 2.]])
>>> z = np.ceil(x)
>>> z
array([[ 1., 3.],
[ 2., 3.]])
>>> t = np.floor(x)
>>> t
array([[ 1., 2.],
[ 1., 2.]])
>>> a = np.rint(x)
>>> a
array([[ 1., 2.],
[ 1., 3.]])
To make one of this in to int, or one of the other types in numpy, astype (as answered by BrenBern):
a.astype(int)
array([[1, 2],
[1, 3]])
>>> y.astype(int)
array([[1, 2],
[1, 2]])

fhtuft
- 966
- 5
- 8
-
2Exactly what I was looking for. `astype` is often too generic, and I think it probably is more useful when doing intx - inty conversions. When I want to do float - int conversion being able to choose the kind of rounding is a nice feature. – Bakuriu Sep 11 '12 at 07:03
-
15So the simplest way to safely convert almost-ints like `7.99999` to ints like `8`, is `np.rint(arr).astype(int)`? – endolith Oct 12 '12 at 18:53
-
-
2
-
19
you can use np.int_
:
>>> x = np.array([[1.0, 2.3], [1.3, 2.9]])
>>> x
array([[ 1. , 2.3],
[ 1.3, 2.9]])
>>> np.int_(x)
array([[1, 2],
[1, 2]])

Hackaholic
- 19,069
- 5
- 54
- 72
17
If you're not sure your input is going to be a Numpy array, you can use asarray
with dtype=int
instead of astype
:
>>> np.asarray([1,2,3,4], dtype=int)
array([1, 2, 3, 4])
If the input array already has the correct dtype, asarray
avoids the array copy while astype
does not (unless you specify copy=False
):
>>> a = np.array([1,2,3,4])
>>> a is np.asarray(a) # no copy :)
True
>>> a is a.astype(int) # copy :(
False
>>> a is a.astype(int, copy=False) # no copy :)
True

1''
- 26,823
- 32
- 143
- 200