Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting object to float loses too much precision - pandas

I'm trying to plot a DataFrame using pandas but it's not working (see this similar thread for details). I think part of the problem might be that my DataFrame seems to be made of objects:

>>> df.dtypes
Field          object
Moment         object
Temperature    object

However, if I were to convert all the values to type float, I lose a lot of precision. All the values in column Moment are of the form -132.2036E-06 and converting to float with df1 = df.astype(float) changes it to -0.000132.

Anyone know how I can preserve the precision?

like image 617
thosphor Avatar asked Feb 14 '23 08:02

thosphor


1 Answers

You can do this to change the displayed precision

In [1]: df = DataFrame(np.random.randn(5,2))

In [2]: df
Out[2]: 
          0         1
0  0.943371  0.171686
1  1.508525  0.005589
2 -0.764565  0.259490
3 -1.059662 -0.837602
4  0.561804 -0.592487

[5 rows x 2 columns]

In [3]: pd.set_option('display.precision',12)

In [4]: df
Out[4]: 
               0              1
0  0.94337126946  0.17168604324
1  1.50852519105  0.00558907755
2 -0.76456509501  0.25948965731
3 -1.05966206139 -0.83760201886
4  0.56180449801 -0.59248656304

[5 rows x 2 columns]
like image 151
Jeff Avatar answered Feb 17 '23 01:02

Jeff