Unidata/cftime

Wrong result with `date2num`

shaharkadmiel opened this issue · 6 comments

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

cftime cannot currently handle fractional seconds in the reference date.

That is unfortunate...

It's a bug - it should work.

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

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

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