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

Precision error when using Timedelta.total_seconds method #19458

Closed
nvgoldin opened this Issue Jan 30, 2018 · 9 comments

Comments

Projects
None yet
3 participants
@nvgoldin

nvgoldin commented Jan 30, 2018

Code Sample

>>> import pandas as pd
>>> block = pd.Timedelta('30S')
>>> block.total_seconds()
30.000000000000004
>>> 

Problem description

When using Timedelta.total_seconds I sometime encounter a small precision error (1e-15), which can be significant in some scenarios (for instance when later dividing the results and expecting an integer). The workaround is to always use a rounding method, which makes the code cumbersome.

Expected Output

A float or integer representing the seconds precisely.

Output of pd.show_versions()

pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-5-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: en_US.utf8
LANG: en_US.UTF-8
LOCALE: None.None

pandas: 0.22.0
pytest: None
pip: 9.0.1
setuptools: 38.4.0
Cython: None
numpy: 1.14.0
scipy: None
pyarrow: None
xarray: None
IPython: None
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: None
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 0.999999999
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback

This comment has been minimized.

Contributor

jreback commented Jan 30, 2018

we are doing this.

In [9]: pd.Timedelta('30S').value * 1e-9
Out[9]: 30.000000000000004

this would avoid the issue

In [11]: pd.Timedelta('30S').value / 1e9
Out[11]: 30.0

look in pandas/_libs/tslibs/timedelta.pyx for the code

would take a PR for this.

@jreback jreback added this to the Next Major Release milestone Jan 30, 2018

@CruiseDevice

This comment has been minimized.

CruiseDevice commented Jan 30, 2018

I would like to work on this. Can you please guide me?

@CruiseDevice

This comment has been minimized.

CruiseDevice commented Jan 30, 2018

From the path to the file you gave i found the function i need to modify.

@jreback

This comment has been minimized.

@nvgoldin

This comment has been minimized.

nvgoldin commented Jan 31, 2018

@jreback, thanks for the references - why is the pd.Timedelta('30S').value / 1e9 an issue? How would you expect it to be solved for both issues?

@CruiseDevice - LMK if your not taking it. I'll try freeing some time for it.

@CruiseDevice

This comment has been minimized.

CruiseDevice commented Jan 31, 2018

@nvgoldin Creating development environment on my machine took time. I have one question though Do i have to create a new branch for this? or should i make changes directly to my branch and then create a PR?

@CruiseDevice

This comment has been minimized.

CruiseDevice commented Jan 31, 2018

I would like to spend sometime on this. If i am not able to do it you can happily take this. I will let you know.

CruiseDevice added a commit to CruiseDevice/pandas that referenced this issue Jan 31, 2018

@jreback jreback modified the milestones: Next Major Release, 0.23.0 Feb 1, 2018

@CruiseDevice

This comment has been minimized.

CruiseDevice commented Feb 5, 2018

@nvgoldin Hey. If you want you can take this issue. I am busy on something else.

@nvgoldin

This comment has been minimized.

nvgoldin commented Feb 20, 2018

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment