-1
votes

I'm trying to fill an array with integers, but it seems like numpy array keep turning the integers into floats. Why is this happening and how do I stop this?

arr = np.empty(9)
arr[3] = 7
print(arr[3])
>>>7.0
1
Have you checked the data type of arr before assigning arr[3]? - Mr. T
The default dtype for np.empty is float: docs.scipy.org/doc/numpy-1.14.0/reference/generated/… - ayhan
@AnkitJaiswal That is not, and never was, an appropriate reason for closure. At best, it's a reason to downvote, which you could have done instead. - E_net4 the curator
thanks, @user2285236, I will have to specify the data type when using np.empty() - Ruiyang Li

1 Answers

6
votes

NumPy arrays, unlike Python lists, can contain only a single type, which (as far as I know) is set at creation time. Everything you put into the array gets converted to that type.

By default, the data type is assumed to be float. To set another type, you can pass dtype to the empty function like this:

>>> arr = np.empty(9, dtype=int)
>>> arr[3] = 7
>>> arr[3]
7