Skip to content

BUG: Roundoff error in cumsum() of large array of float32 #47488

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
2 of 3 tasks
d-cogswell opened this issue Jun 23, 2022 · 3 comments
Closed
2 of 3 tasks

BUG: Roundoff error in cumsum() of large array of float32 #47488

d-cogswell opened this issue Jun 23, 2022 · 3 comments
Labels
Closing Candidate May be closeable, needs more eyeballs Upstream issue Issue related to pandas dependency

Comments

@d-cogswell
Copy link

d-cogswell commented Jun 23, 2022

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 numpy as np
import pandas as pd

# Generate a long sequence of random numbers
np.random.seed(1)
series = pd.Series(np.random.randint(0, high=1e4, size=1000000)/1e4)

# The last element of cumsum() should be the same as sum()
print(f"cumsum(float64): {series.astype('float64').cumsum().iat[-1]}")
print(f"cumsum(float32): {series.astype('float32').cumsum().iat[-1]}")
print(f"   sum(float32): {series.astype('float32').sum()}")

# Sum series as float32
sum = np.float32(0)
for i in series:
    sum += np.float32(i)
sum

Issue Description

I encountered this when working with a large DataFrame with float32. It appears that cumsum() is accumulating roundoff error (by operating on float32?), while sum() apparently does not accumulate roundoff error. There are no warnings about precision in the documentation.

Expected Behavior

I expected cumsum(float32) be as accurate as sum(float32), and to force the use of high precision if necessary.

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.13.0-51-generic
Version : #58~20.04.1-Ubuntu SMP Tue Jun 14 11:29:12 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.3
numpy : 1.23.0
pytz : 2022.1
dateutil : 2.8.2
setuptools : 44.0.0
pip : 20.0.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.4.0
pandas_datareader: None
bs4 : None
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
markupsafe : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@d-cogswell d-cogswell added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 23, 2022
@phofl
Copy link
Member

phofl commented Jun 24, 2022

Hi, thanks for your report.

We are dispatching to numpy here. It looks like this is on the numpy side:

print(f"cumsum(float64): {np.cumsum(series.astype('float64').values)[-1]}")
print(f"cumsum(float32): {np.cumsum(series.astype('float32').values)[-1]}")

Could you check the issues at numpy?

@phofl phofl added Upstream issue Issue related to pandas dependency Closing Candidate May be closeable, needs more eyeballs and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Jun 24, 2022
@d-cogswell
Copy link
Author

Ah, it's a numpy issue that was reported here. Looks like they updated the documentation but didn't change anything else. Do you think it's worth sharing this example?

@phofl
Copy link
Member

phofl commented Jul 2, 2022

Don't think so, they seem to be aware of this. Closing here

@phofl phofl closed this as completed Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Closing Candidate May be closeable, needs more eyeballs Upstream issue Issue related to pandas dependency
Projects
None yet
Development

No branches or pull requests

2 participants