Skip to content

Wrong result with date2num #140

@shaharkadmiel

Description

@shaharkadmiel

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions