Skip to content
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

Wrong result with date2num #140

Closed
shaharkadmiel opened this issue Feb 5, 2020 · 6 comments
Closed

Wrong result with date2num #140

shaharkadmiel opened this issue Feb 5, 2020 · 6 comments

Comments

@shaharkadmiel
Copy link

cftime.date2num is returning a wrong result. See example below:

import cftime as cf
import pandas as pd

print('cftime version:', cf.__version__)

reftime = pd.to_datetime('2018-01-23 09:31:42.94')
t = pd.to_datetime('2018-01-23 09:27:10.950000')

# seconds since a reference time
cf_delta = cf.date2num(t.to_pydatetime(), f'seconds since {reftime}')

print('cftime date2num:', cf_delta)

Output:

cftime version: 1.0.4.2
cftime date2num: -271.05

The answer should be:

# using pandas timedelta calculation
pd_delta = (t - reftime).total_seconds()
print('pandas timedelta:', pd_delta)

# manual calculation
delta = (
    (t.minute * 60 + t.second + t.microsecond * 1e-6) -
    (reftime.minute * 60 + reftime.second + reftime.microsecond * 1e-6)
)
print('manual timedelta:', delta)

Output:

pandas timedelta: -271.99
manual timedelta: -271.99
@jswhit2
Copy link
Contributor

jswhit2 commented Feb 5, 2020

cftime cannot currently handle fractional seconds in the reference date.

@shaharkadmiel
Copy link
Author

That is unfortunate...

@jswhit2
Copy link
Contributor

jswhit2 commented Feb 5, 2020

It's a bug - it should work.

@shaharkadmiel
Copy link
Author

I belive the bug is here:

totaltime = td.microseconds + (td.seconds + td.days * 24 * 3600) * 1.e6

Even python datetime arithmetic can handle this:

delta = t.to_pydatetime() - reftime.to_pydatetime()
print(type(delta), delta.total_seconds())

Output:

<class 'datetime.timedelta'> -271.99

@jswhit2
Copy link
Contributor

jswhit2 commented Feb 5, 2020

It's a simple fix, PR coming...

jswhit added a commit that referenced this issue Feb 5, 2020
fix for issue #140 (fraction seconds in reference date)
@jswhit
Copy link
Collaborator

jswhit commented Feb 5, 2020

Fixed by PR #141 - works now in master and will be in 1.1.0 release.

@jswhit jswhit closed this as completed Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants