Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas ".convert_objects(convert_numeric=True)" deprecated [duplicate]

Tags:

python

pandas

I have this line in my code which converts my data to numeric...

data["S1Q2I"] = data["S1Q2I"].convert_objects(convert_numeric=True) 

The thing is that now the new pandas release (0.17.0) said that this function is deprecated.. This is the error:

FutureWarning: convert_objects is deprecated.   Use the data-type specific converters pd.to_datetime,  pd.to_timedelta and pd.to_numeric.  data["S3BD5Q2A"] = data["S3BD5Q2A"].convert_objects(convert_numeric=True) 

So, I went to the new documentation and I couldn't find any examples of how to use the new function to convert my data...

It only says this:

"DataFrame.convert_objects has been deprecated in favor of type-specific functions pd.to_datetime, pd.to_timestamp and pd.to_numeric (new in 0.17.0) (GH11133)."

Any help would be nice!

like image 569
guidebortoli Avatar asked Oct 14 '15 13:10

guidebortoli


People also ask

How do you change str to float in pandas?

pandas Convert String to FloatUse pandas DataFrame. astype() function to convert column from string/int to float, you can apply this on a specific column or on an entire DataFrame. To cast the data type to 54-bit signed float, you can use numpy. float64 , numpy.

How do I convert items to numeric in pandas?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric(). This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do I change data type from float to object?

How do you turn an object into a float? Use the astype() Method to Convert Object to Float in Pandas. Use the to_numeric() Function to Convert Object to Float in Pandas. Related Article - Pandas DataFrame.

How do you select only the value of an integer in Python?

So for taking integer input we have to type cast those inputs into integers by using Python built-in int() function.


2 Answers

As explained by @EvanWright in the comments,

data['S1Q2I'] = pd.to_numeric(data['S1Q2I']) 

is now the prefered way of converting types. A detailed explanation in of the change can be found in the github PR GH11133.

like image 91
Hooked Avatar answered Oct 07 '22 18:10

Hooked


You can effect a replacement using apply as done here. An example would be:

>>> import pandas as pd >>> a = pd.DataFrame([{"letter":"a", "number":"1"},{"letter":"b", "number":"2"}]) >>> a.dtypes letter    object number    object dtype: object >>> b = a.apply(pd.to_numeric, errors="ignore") >>> b.dtypes letter    object number     int64 dtype: object >>>  

But it sucks in two ways:

  1. You have to use apply rather than a non-native dataframe method
  2. You have to copy to another dataframe--can't be done in place. So much for use with "big data."

I'm not really loving the direction pandas is going. I haven't used R data.table much, but so far it seems superior.

I think a data table with native, in-place type conversion is pretty basic for a competitive data analysis framework.

like image 31
abalter Avatar answered Oct 07 '22 18:10

abalter