简体   繁体   English

Pandas df.astype('float32') 失去了很多精度

[英]Pandas df.astype('float32') loses a lot of precision

Why does a float64 value 123456789.0 in a Pandas.DataFrame gets converted to 123456792.0 , preserving only 7 significant digits?为什么 Pandas.DataFrame 中的float64123456789.0被转换为123456792.0 ,只保留 7 个有效数字?

import pandas as pd

df = pd.DataFrame([123456789.0])

#              0
# 0  123456789.0

df = df.astype('float32')

#              0
# 0  123456792.0

Essentially, float32 is numpy 's dtype .本质上, float32numpydtype 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).在将float64转换为float32时,您看到精度有所不同的原因是,无法使用 32 位 dtype(1 个符号位、8 位指数、23 位尾数)的float32准确表示123456789.0

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 .通常, float32需要float64表示数值所需内存的一半,但是与float64相比, float32可以不太准确地表示数字。

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 ).如果您需要表示无法使用 32 位数据类型(如float32表示的特定数字,请使用更高精度的数据类型( float64 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM