-1

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
4
  • 1
    Have you checked the data type of arr before assigning arr[3]? Commented Jul 31, 2018 at 7:23
  • 3
    The default dtype for np.empty is float: docs.scipy.org/doc/numpy-1.14.0/reference/generated/… Commented Jul 31, 2018 at 7:24
  • 3
    @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. Commented Jul 31, 2018 at 7:44
  • thanks, @user2285236, I will have to specify the data type when using np.empty() Commented Aug 1, 2018 at 21:35

1 Answer 1

10

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.