简体   繁体   English

在 Pandas 中使用 astype 不会给出预期的结果

[英]Using astype in Pandas does not give the expected result

I'm trying to convert a float to int in a Pandas dataframe.我正在尝试在 Pandas dataframe 中将浮点数转换为 int。 I usually use .astype('int64') but, in this case, it is not working.我通常使用.astype('int64')但在这种情况下,它不起作用。 This is the code I'm using:这是我正在使用的代码:

import pandas as pd
d = {'test': [1]}
df = pd.DataFrame(columns= ['test'], data =d)

df['test'] = 60590820065001969.0
df['test'].astype('int64')

This is the result I get:这是我得到的结果:

0    60590820065001968
Name: test, dtype: int64

Please notice how those numbers are different (the float ends with 69 and the integer version ends with 68).请注意这些数字有何不同(浮点数以 69 结尾,integer 版本以 68 结尾)。

If i try a smaller number, by removing the first 2 digits, then it works fine:如果我尝试一个较小的数字,通过删除前 2 位数字,那么它工作正常:

df['test'] = 590820065001969.0
df['test'].astype('int64')

Gives me:给我:

0    590820065001969
Name: test, dtype: int64

Which makes me think it might have something to do with the number size, but I'm not sure what it is.这让我觉得它可能与数字大小有关,但我不确定它是什么。 Can anyone spot the problem here?任何人都可以在这里发现问题吗? By the way, I'm using Python 3.顺便说一句,我正在使用 Python 3。

60590820065001969.0 is too large for python to be represented precisely in the floating point format. 60590820065001969.0太大,无法以浮点格式精确表示 python。 Hence, python picks the nearest value that it's sure of representing correctly.因此,python 选择它可以确定正确表示的最接近的值。

Using decimal library使用decimal

In [16]: import decimal

In [17]: a = decimal.Decimal("60590820065001969.0")

In [18]: int(a)
Out[18]: 60590820065001969

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

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