Skip to content

BUG: Unexpected rounding behaviour #60914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
Tim-Kirkwood opened this issue Feb 11, 2025 · 5 comments
Closed
3 tasks done

BUG: Unexpected rounding behaviour #60914

Tim-Kirkwood opened this issue Feb 11, 2025 · 5 comments
Assignees
Labels
Docs Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@Tim-Kirkwood
Copy link

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd
print (pd.DataFrame([[-0.025]]).round(2).values)
print (pd.DataFrame([[-0.025001]]).round(2).values)
print (round(-0.025, 2))
print (round(-0.025001, 2))

#output:
#[[-0.02]]
#[[-0.03]]
#-0.03
#-0.03

Issue Description

I would expect all values to be -0.03 but pandas rounds -0.025 to -0.02. I suspect this is due to bankers rounding, which would be fine, but it also looks like pandas doesn't mimic the native python rounding behaviour, which rounds both to -0.03 - this might trip people up.

Expected Behavior

Round -0.025 to -0.03, or if this explicitly avoided as part of your rounding strategy, at least make your strategy match the python round function.

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.10.15.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.22631
machine : AMD64
processor : Intel64 Family 6 Model 186 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en
LOCALE : English_United Kingdom.1252

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 75.1.0
pip : 22.3.1
Cython : None
pytest : 8.3.4
hypothesis : None
sphinx : 7.3.7
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.4
IPython : 8.27.0
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : 1.3.7
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.9.2
numba : None
numexpr : 2.10.1
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.14.1
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : 2.4.1
pyqt5 : None

@Tim-Kirkwood Tim-Kirkwood added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 11, 2025
@Liam3851
Copy link
Contributor

Series.round explicitly delegates this to numpy for numpy dtyped-arrays (above your array will be np.float64 by default), and more generally follows the numpy convention here: https://numpy.org/doc/stable/reference/generated/numpy.round.html.

@rhshadrach
Copy link
Member

We also use PyArrow's compute on e.g. dtype=float64[pyarrow] which has the same behavior as NumPy.

https://arrow.apache.org/docs/python/generated/pyarrow.compute.round.html

I think adding that we use round-to-even to the documentation of Series.round and DataFrame.round would be welcome!

@rhshadrach rhshadrach added Docs Numeric Operations Arithmetic, Comparison, and Logical operations and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Feb 12, 2025
@palbha
Copy link

palbha commented Feb 17, 2025

I would like to take this up as my first PR -
@rhshadrach I see the round to even already mentioned though - Can you suggest if there was anything specific you were thinking of ?

Image

@palbha
Copy link

palbha commented Feb 17, 2025

take

@rhshadrach
Copy link
Member

@palbha - thanks! I think I missed this line, and don't think there is anything further to do here. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Docs Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

No branches or pull requests

4 participants