7

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?

Community
  • 1
  • 1
thosphor
  • 2,493
  • 7
  • 26
  • 42
  • 2
    this is almost certainly just the printed precision. Do ``df.values`` after you astype will show the raw numpy values. You can also astype with ``np.float64`` if you are on 32-bit platform to make them 64-bit. – Jeff Dec 16 '13 at 15:40
  • Different formatting doesn't mean your values have lost some digits. Compare df1 against float('-132.2036E-06'). – Lorenzo Gatti Dec 16 '13 at 15:43
  • @Jeff @Lorenzo Thanks guys, didn't realise the printed value would ever be different from the stored value. `df.values` did indeed show no such loss of precision. – thosphor Dec 16 '13 at 16:35

1 Answers1

9

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]
Jeff
  • 125,376
  • 21
  • 220
  • 187