我发表这篇文章是因为我认为这是答案,因为我在这里找不到类似的问题和答案。
我期望pandas的round
方法将0.5四舍五入为1。
>>> import pandas as pd
>>> pd.Series([0.5, 1.5, 2.5, 3.5, 4.5]).round()
0 0.0
1 2.0
2 2.0
3 4.0
4 4.0
dtype: float64
更加奇怪的是:Python的round方法在Python 2.7和3.6中有不同的行为:
Python 2.7:
>>> [round(x) for x in [0.5, 1.5, 2.5, 3.5, 4.5]]
[1.0, 2.0, 3.0, 4.0, 5.0]
Python 3.6:
>>> [round(x) for x in [0.5, 1.5, 2.5, 3.5, 4.5]]
[0, 2, 2, 4, 4]
这是否与浮点数表示或我的平台(Mac OS X)有关?