Skip to content

Summing nanoseconds time delta to timestamp #43764

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
1 of 3 tasks
deponovo opened this issue Sep 27, 2021 · 7 comments · Fixed by #45108
Closed
1 of 3 tasks

Summing nanoseconds time delta to timestamp #43764

deponovo opened this issue Sep 27, 2021 · 7 comments · Fixed by #45108
Assignees
Labels
Bug Datetime Datetime data dtype Timedelta Timedelta data type
Milestone

Comments

@deponovo
Copy link
Contributor

deponovo commented Sep 27, 2021

  • 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 master branch of pandas.

Reproducible Example

import pandas as pd
res1 = (pd.Timestamp(1001) == pd.Timedelta(seconds=1e-9) + pd.Timestamp(1000))  # False, but should be True
res2 = (pd.Timestamp(1001) == pd.Timestamp(1) + pd.Timedelta(seconds=1000e-9))  # True
res1 == res2  # False, should be True
# this sum works properly `pd.Timestamp(1) + pd.Timedelta(nanoseconds=1)`

Issue Description

Adding Timedeltas that have nanoseconds defined as a float do not work properly for timedelta values in the [1-999] range.
This report is a result of this SO question

Expected Behavior

all([pd.Timestamp(1000 + x) == (pd.Timedelta(seconds=x*1e-9) + pd.Timestamp(1000)) for x in range(1000)]) should be True

Or any equivalent not using the explicit default nanoseconds unit or the nanoseconds keyword as in: pd.Timedelta(1) or pd.Timedelta(nanoseconds=1)

Installed Versions

INSTALLED VERSIONS

commit : c7f7443
python : 3.8.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.18362
machine : AMD64
processor : Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : de_DE.cp1252
pandas : 1.3.1
numpy : 1.20.3
pytz : 2021.1
dateutil : 2.8.1
pip : 21.1.2
setuptools : 49.6.0.post20210108
Cython : None
pytest : 6.2.4
hypothesis : None
sphinx : 4.0.2
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.1
IPython : 7.24.1
pandas_datareader: None
bs4 : None
bottleneck : 1.3.2
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.1
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@deponovo deponovo added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 27, 2021
@FObersteiner
Copy link

FObersteiner commented Sep 27, 2021

To clarify a bit more, you can create a pandas Timedelta with nanoseconds specified;

td = pd.Timedelta(nanoseconds=1)
print(td.seconds, td.microseconds, td.nanoseconds)
# 0 0 1

You can also specify a certain number of microseconds by providing the microseconds as fractional seconds:

td = pd.Timedelta(seconds=1e-6)
print(td.seconds, td.microseconds, td.nanoseconds)
# 0 1 0

However what does not work is specifying nanoseconds like this:

td = pd.Timedelta(seconds=1e-9)
print(td.seconds, td.microseconds, td.nanoseconds)
# 0 0 0

So the point here to me is: everything that pandas Timedelta inherited from Python's timedelta works as expected. But when it comes to the additional feature, the nanoseconds attribute, a correct conversion from some inputs seems to be missing.


>>> sys.version
'3.9.4 (tags/v3.9.4:1f2e308, Apr  6 2021, 13:40:21) [MSC v.1928 64 bit (AMD64)]'
>>> pd.__version__
'1.3.3'

@jbrockmendel
Copy link
Member

res1 = (pd.Timestamp(1001) == pd.Timedelta(seconds=1-9) + pd.Timestamp(1000)) # False, but should be True

First off I'm guessing you want 1e-9 instead of 1-9. Fixing that doesn't actually make a difference bc the Timedelta constructor casts 1e-9 to integer, in this case 0.

Could take a PR either a) raising if a non-integer is passed to Timedelta (depends on how much that check affects perf) or b) documenting this requirement

@jreback
Copy link
Contributor

jreback commented Sep 29, 2021

this is a duplicate issue
pls have a search of the tracker

@deponovo
Copy link
Contributor Author

deponovo commented Sep 29, 2021

@jbrockmendel fixed the typo.

Timedelta constructor casts 1e-9 to integer, in this case 0.

This is not consistent with:
td = pd.Timedelta(seconds=1e-6)
as mentioned by @MrFuppes and also exemplified in my original message:

res2 = (pd.Timestamp(1001) == pd.Timestamp(1) + pd.Timedelta(seconds=1000e-9)) # True

@FObersteiner
Copy link

FObersteiner commented Sep 29, 2021

this is a duplicate issue pls have a search of the tracker

@jreback would not surprise me - but I couldn't find an exact dupe by searching the issues (also not knowing where exactly the described behavior originates from). If you have one in mind, please share.

potentially related:

@mroeschke mroeschke added Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 1, 2021
@jbrockmendel jbrockmendel added the Timedelta Timedelta data type label Nov 3, 2021
@deponovo
Copy link
Contributor Author

Take

@deponovo
Copy link
Contributor Author

@mroeschke @jbrockmendel @jreback would it be ok if I solve this issue together with #40946 in one PR?

@jreback jreback added this to the 1.4 milestone Dec 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants