我正在调试并遇到以下奇怪的行为。
我计算了一个包含所有完全相同数字的大熊猫系列的平均值。但是,pd.mean()
会给出不同的数字。
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
答案 0 :(得分:2)
尝试使用np.isclose()
tmm[20]== tmm.mean()
False
np.isclose(tmm[20], tmm.mean())
True