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