Skip to content

Avoiding automatic sanitization to nanoseconds in conversion to DataArray when dealing with out-of-range timestamps #6712

Answered by spencerkclark
leez4rd asked this question in Q&A
Discussion options

You must be logged in to vote

No worries -- indeed dealing with datetimes is complicated. In xarray the standard practice in this situation is to use cftime.datetime objects (see more information in xarray's documentation here). cftime.datetime objects are analogous to standard library datetimes, except they support both standard and non-standard calendars.

Here is how I would use them in your example:

import xarray as xr 
import numpy as np

from cftime import DatetimeProlepticGregorian
from datetime import timedelta

decyrs = [1260, 1320, 1640, 1980, 2005]  # years AD to convert to datetime64 objects 

dates = np.empty([5],dtype="O")

for i in range(5):
    start = decyrs[i]                   # this is the input (ti…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by leez4rd
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants