简体   繁体   English

将浮动转换为pandas中的字符串时精度损失

[英]Loss of precision while converting floats to strings in pandas

I am converting in Python some floats (some shorter some longer) to strings and getting unexpected (?) results: 我在Python中将一些浮点数(有些更短一些更长)转换为字符串并获得意外(?)结果:

Case 1 情况1

pd.options.display.float_format = '{:.2f}'.format
pd.DataFrame({'x': [12345.67]})

           x
0   12345.67

Case 2 案例2

pd.DataFrame({'x': [1234589890808980.67]})

                      x
0   1234589890808980.75

Case 3 案例3

pd.DataFrame({'x': [1234589890878708980.67]})

                         x
0   1234589890878708992.00

I even tried dtypes np.float128 and np.longdouble , but without avail. 我甚至尝试过dtypes np.float128np.longdouble ,但没有用。

Can somebody please explain what is happenning here and is "proper" conversion posiible in Cases 2 and 3? 有人可以解释一下这里发生了什么,并且案例2和3中的“正确”转换是否可行?

Thanks! 谢谢!

I'm afraid this "issue" happens in the Python (instead of pandas) side. 我担心这个“问题”发生在Python(而不是pandas)方面。 When you have some instant values like 1234589890878708980.67 it's recognized as float and loses precision instantly, eg: 当你有一些瞬时值,如1234589890878708980.67它被识别为float并立即失去精度,例如:

>>> 1234589890878708980.67
1.234589890878709e+18
>>> 1234589890878708980.67 == 1234589890878708980.6712345
True

You might try something like decimal.Decimal : 您可以尝试使用decimal.Decimal

>>> import decimal
>>> pd.DataFrame({'x': [decimal.Decimal('1234589890808980.67')]})
                     x
0  1234589890808980.67

EDITED: 编辑:

OP's added a few questions in the comment. OP在评论中添加了一些问题。

However, do I understand it right that for this method work correctly the value should be string in the first place? 但是,我是否理解为正确的方法,这个值应该首先是字符串?

Yes :) 是的:)

What if it's float read from csv file? 如果从csv文件读取浮点数怎么办?

AFAIK Python's csv reader shall not have performed any type conversion, and you'll get strings that could be later converted freely. AFAIK Python的csv阅读器不应该执行任何类型转换,并且您将获得可以在以后自由转换的字符串。 Otherwise if you're using pandas.read_csv , you could try setting the dtype and float_precision arguments (you could also ask pandas to load plain strings, and have the values converted later yourself). 否则,如果你使用pandas.read_csv ,你可以尝试设置的dtypefloat_precision参数(你也可以问大熊猫装入普通字符串,并有转换后自己值)。

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

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