df.mean不是该系列的真正意义吗?

时间:2016-08-14 22:46:30

标签: python pandas

我正在调试并遇到以下奇怪的行为。 我计算了一个包含所有完全相同数字的大熊猫系列的平均值。但是,pd.mean()会给出不同的数字。

问题1:为什么这个系列的意思是不同的数字?

问题2:tmm[-1]== tmm.mean()现在提供False。有什么方法可以忽略这个小差异并使结果成真吗?我不喜欢abs(tmm[-1]-tmm.mean()) < xxx

方法因为不确定如何定义xxx

import pandas as pd
import decimal

tmm = pd.Series(14.9199999999999999289457264239899814128875732421875,
                index=range(30))
for t in tmm:
    print(decimal.Decimal(t))
print('mean is')
print(decimal.Decimal(tmm.mean()))

结果:

14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
14.9199999999999999289457264239899814128875732421875
mean is
14.9200000000000034816594052244909107685089111328125

1 个答案:

答案 0 :(得分:2)

尝试使用np.isclose()

tmm[20]== tmm.mean()

False

np.isclose(tmm[20], tmm.mean())
True