Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I convert a Dataframe full of decimals to floats?

The main problem I have right now is that DataFrame.quantile() doesn't work with Decimals.

I can convert my Dataframe to floats by using df.convert_objects(convert_numeric=True), but this generates a deprecation warning and the suggested replacement infer_objects() doesn't work.

As an alternative, I could use Dataframe.round on my input data instead of converting them to Decimals. I assume that this should be safe for direct comparisons (I am indexing on a column to subtract two dataframes from each other, e.g. df1.set_index('Time') - df2.set_index('Time')) but I can't be certain.

like image 483
Nan Lin Avatar asked Mar 01 '18 16:03

Nan Lin


1 Answers

You can apply pd.to_numeric to an entire dataframe:

df = df.apply(pd.to_numeric, downcast='float')
like image 120
jpp Avatar answered Nov 03 '22 23:11

jpp