Convert decimal years to datetime64 data #6868
-
Dear everyone, I am trying to use the gridded Monthly Land + Ocean dataset from Berkeley earth. The time steps are saved as decimal years. I have tried various approaches to convert these to datetime64 data, but I have no success so far. Is there an implemented version which could perform this conversion? These are the time steps I would like to convert.
Best regards |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
it's my understanding that the time units In [30]: def decimal_to_datetime(value):
...: year = int(value)
...: remainder = value - year
...: base = datetime.datetime(year , 1, 1)
...: result = base + datetime.timedelta(seconds=(base.replace(year=base.year + 1) - base).total_seconds() * remainder)
...: return result
...:
In [31]: decimal_to_datetime(1850.041667)
Out[31]: datetime.datetime(1850, 1, 16, 5, 0, 10, 511999)
In [32]: times
Out[32]:
<xarray.DataArray (time: 6)>
array([1850.041667, 1850.125 , 1850.208333, 2022.291667, 2022.375 ,
2022.458333])
Dimensions without coordinates: time
In [33]: times.data
Out[33]:
array([1850.041667, 1850.125 , 1850.208333, 2022.291667, 2022.375 ,
2022.458333])
In [34]: dates = [decimal_to_datetime(value) for value in times.data]
In [35]: dates
Out[35]:
[datetime.datetime(1850, 1, 16, 5, 0, 10, 511999),
datetime.datetime(1850, 2, 15, 15, 0),
datetime.datetime(1850, 3, 18, 0, 59, 49, 488001),
datetime.datetime(2022, 4, 17, 11, 0, 10, 511999),
datetime.datetime(2022, 5, 17, 21, 0),
datetime.datetime(2022, 6, 17, 6, 59, 49, 488001)]
In [36]: times.data = dates
In [37]: times
Out[37]:
<xarray.DataArray (time: 6)>
array(['1850-01-16T05:00:10.511999000', '1850-02-15T15:00:00.000000000',
'1850-03-18T00:59:49.488001000', '2022-04-17T11:00:10.511999000',
'2022-05-17T21:00:00.000000000', '2022-06-17T06:59:49.488001000'],
dtype='datetime64[ns]')
Dimensions without coordinates: time would this suffice for your use case? |
Beta Was this translation helpful? Give feedback.
@mgorfer,
it's my understanding that the time units
units: year A.D.
don't follow CF conventions. Consequently, xarray doesn't know how to decode these values out of the box. One solution is to convert your decimal values into datetime objects. The following code snippet is adapted from this Stackoverflow post