Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 297
user2590144 Avatar asked Apr 17 '14 21:04

user2590144


People also ask

What is Astype in Python pandas?

The astype() method returns a new DataFrame where the data types has been changed to the specified type. You can cast the entire DataFrame to one specific data type, or you can use a Python Dictionary to specify a data type for each column, like this: { 'Duration': 'int64', 'Pulse' : 'float', 'Calories': 'int64' }

What does Astype do in Python?

The astype() function is used to cast a pandas object to a specified data type. Use a numpy. dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy.

How do you convert Dtype to Python?

In order to change the dtype of the given array object, we will use numpy. astype() function. The function takes an argument which is the target data type.


1 Answers

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])
like image 182
unutbu Avatar answered Sep 19 '22 23:09

unutbu