user2590144
user2590144

Reputation: 281

Python 'astype' not working

I am currently using Spyder from Anaconda and I am trying to convert an array containing type float to type int:

x = np.array([1, 2, 2.5])
x.astype(int)
print x

The result still comes out unchanged:

[1. 2. 2.5]

Thoughts?

Upvotes: 8

Views: 28274

Answers (1)

unutbu
unutbu

Reputation: 879471

astype returns a new array. You need to assign the result to x:

In [298]: x = x.astype(int)

In [299]: x
Out[299]: array([1, 2, 2])

Upvotes: 21

Related Questions