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

num2pydate loses millisecond information #144

Closed
jackvreeken opened this issue Mar 8, 2020 · 3 comments
Closed

num2pydate loses millisecond information #144

jackvreeken opened this issue Mar 8, 2020 · 3 comments

Comments

@jackvreeken
Copy link

In 1.1.0 the num2pydate was added, as num2date no longer return a real_datetime. This change was rather annoying for me, as I was relying on the datetime'ish properties of real_datetime (e.g. being an instance of it). But as far as the bug is concerned:

One of my dates is 1970-01-01 02:00:00 and 13 milliseconds.

  • When reading it with num2date in 1.0.4.2 I would get a Python datetime-ish (real_datetime) object with the right value.
  • When reading it with num2date in 1.1.0 I would get a non-Python datetime object with the right value (a cftime.DatetimeGregorian instance I think).
  • When reading it with num2pydate in 1.1.0 I would get a Python datetime "1970-01-01 02:00:00", with the milliseconds information lost.
@jswhit
Copy link
Collaborator

jswhit commented Mar 12, 2020

Can you be more specific about what you mean by the 'millisecond information is lost'?

Sorry this changed caused you grief.

@jackvreeken
Copy link
Author

On second look, it seems to be related to parsing of arrays. I remember seeing an issue about this somewhere...

import pprint

from cftime import num2date, num2pydate

time_calendar = 'gregorian'
time_unit = 'hours since 1970-01-01 00:00:00.0 +0000'
time_values = [0.0, 1.0, 2.0, 3.0, 4.0]

# Wrong?
pprint.pprint(num2date(time_values, units=time_unit, calendar=time_calendar))  
# Correct?
pprint.pprint(num2pydate(time_values, units=time_unit, calendar=time_calendar))
# Correct?
pprint.pprint(num2date(time_values[2:3], units=time_unit, calendar=time_calendar))

Output:

array([cftime.DatetimeGregorian(1970-01-01 00:00:00),
       cftime.DatetimeGregorian(1970-01-01 01:00:00),
       cftime.DatetimeGregorian(1970-01-01 02:00:00.13),
       cftime.DatetimeGregorian(1970-01-01 03:00:00),
       cftime.DatetimeGregorian(1970-01-01 04:00:00)], dtype=object)
array([datetime.datetime(1970, 1, 1, 0, 0),
       datetime.datetime(1970, 1, 1, 1, 0),
       datetime.datetime(1970, 1, 1, 2, 0),
       datetime.datetime(1970, 1, 1, 3, 0),
       datetime.datetime(1970, 1, 1, 4, 0)], dtype=object)
array([cftime.DatetimeGregorian(1970-01-01 02:00:00)], dtype=object)

@jswhit
Copy link
Collaborator

jswhit commented Mar 14, 2020

This has been fixed by PR #146

@jswhit jswhit closed this as completed Mar 15, 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

2 participants