Converting cftime.datetime
objects to np.datetime64
values through astype
#5107
Labels
cftime.datetime
objects to np.datetime64
values through astype
#5107
The discussion of the use of the
indexes
property in #5102 got me thinking about this StackOverflow answer. For a while I have thought that my answer there isn't very satisfying, not only because it relies on this somewhat obscureindexes
property, but also because it only works on dimension coordinates -- i.e. something that would be backed by an index.Describe the solution you'd like
It would be better if we could do this conversion with
astype
, e.g.da.astype("datetime64[ns]")
. This would allow conversion todatetime64
values for allcftime.datetime
DataArrays -- dask-backed or NumPy-backed, 1D or ND -- through a fairly standard and well-known method. To my surprise, while you do not get the nice calendar-switching warning thatCFTimeIndex.to_datetimeindex
provides, this actually already kind of seems to work (?!):NumPy obviously does not officially support this -- nor would I expect it to -- so I would be wary of simply documenting this behavior as is. Would it be reasonable for us to modify
xarray.core.duck_array_ops.astype
to explicitly implement this conversion ourselves forcftime.datetime
arrays? This way we could ensure this was always supported, and we could include appropriate errors for out-of-bounds times (the NumPy method currently overflows in that case) and warnings for switching from non-standard calendars.The text was updated successfully, but these errors were encountered: