1

Why does a float64 value 123456789.0 in a Pandas.DataFrame gets converted to 123456792.0, preserving only 7 significant digits?

import pandas as pd

df = pd.DataFrame([123456789.0])

#              0
# 0  123456789.0

df = df.astype('float32')

#              0
# 0  123456792.0
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830

1 Answers1

3

Essentially, float32 is numpy's dtype. The reason why you see some difference in the precision when converting float64 to float32 is because 123456789.0 cannot be accurately represented using float32 which is a 32-bit dtype (1 sign bit, 8 bits exponent, 23 bits mantissa).

In general, float32 requires half of the memory that float64 requires to represent a numerical value , however float32 can represent numbers less accurately compared to float64.

Note there is no workaround for this. If you need to represent particular numbers that cannot be represented using a 32-bit dtype like float32, then go for higher precision dtypes (float64).

Giorgos Myrianthous
  • 36,235
  • 20
  • 134
  • 156