Wrong result with `date2num`
shaharkadmiel opened this issue · 6 comments
shaharkadmiel commented
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 commented
cftime cannot currently handle fractional seconds in the reference date.
shaharkadmiel commented
That is unfortunate...
jswhit2 commented
It's a bug - it should work.
shaharkadmiel commented
I belive the bug is here:
Line 226 in ec52b87
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 commented
It's a simple fix, PR coming...