-
Notifications
You must be signed in to change notification settings - Fork 39
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
How to convert cftime.Datetime360Day() object to python datetime? #111
Comments
You can't convert it into a python datetime object, since python datetime does not support that calendar. The Datetime360day object has many of the same attributes and methods as the python datetime object (including year,month,day,hour,minute,second,microsecond) and supports timedelta operations. It would be nice to know what xarray.plot is expecting that Datetime360day doesn't have - perhaps this could be fixed. |
Many thanks for your quick reply. There may not be a function to convert it, but could I write something that can extract the year, month and day from the cftime calendar? Then I can manually concatenate it to create a list ['1979-01-01', '1979-01-02', '1979-01-03' ...] then convert it to a python datatime and perhaps approximate or eliminate the dates that are not possible python datatime (if by "python datatime does not support that calendar" you mean that cftime will sometimes contain dates that do not exist/are not possible in python datatime?). I have other xarray datasets that have the time coordinate in python datetime format so being able to convert from cftime would be extremely handy. Or perhaps there is a way to convert python datatime to a cftime format? I would like to know if there is. xarray plotting error gives the following: As a workaround it would be nice to know if there was a method to manipulate these calendars to force the time to 0:0:0:0 where just the date is important (so that I can concatenate two data arrays to a dataset with one data array having a different time stamp to the other for no reason), so that:
becomes:
So that at least I can combine arrays with the same cftime.x formats. |
We fixed this very recently in xarray by adding an optional dependency on Until then, @ru111, for plotting you have a couple options:
|
Note with the latest version of |
This seems somewhat orthogonal to plotting and could be worth bringing up as an issue in the xarray repo. In pandas speak this is called "normalization" -- see |
Does the |
Exactly the solution I was looking for - thanks @spencerkclark ! |
I've loaded data in xarray format which has a coordinate 'time'. The first 4 values look like:
I'm not quite sure how to interpret all of these numbers (1970, 1, 1, 12, 0, 0, 0, 2, 1) (I couldn't find it in the documentation) but assuming the first 3 digits is year-month-day, I would like to convert this array into a numpy datetime64[nc] format, or some other general datetime format (like pandas datetime), something like:
I want to do this because I can't use the xarray
.plot()
function to view the timeseries since it doesn't seem to understand the cftime format. I would like to get all the datetimes of all of my datasets in the same format because others have datetime64[nc], cftime.DatetimeNoLeap() etc. and I would like to standardise my datetime format to make my analysis easier...The text was updated successfully, but these errors were encountered: