Is it possible to modify dates on the time axis in an xarray dataset? In the following example, I am trying to change one kind of datetime object into another for the first time step.
import xarray as xr import pandas as pd filePath = '/glade/collections/cdg/data/cordex/data/raw/NAM-22i/day/RegCM4/HadGEM2-ES/hist/tasmax/tasmax.hist.HadGEM2-ES.RegCM4.day.NAM-22i.raw.nc' ds = xr.open_dataset(filePath) ds['time'].isel(time=0).values array(cftime.Datetime360Day(1950-01-01 12:00:00), dtype=object) ds['time'].isel(time=0) = pd.date_time(1950,1,1) SyntaxError: cannot assign to function call
Note that I'm ultimately trying to convert all elements to datetime64 objects. And modify their 'dayofyear' values in the process.
You should not be using isel
for assignment:
http://xarray.pydata.org/en/stable/indexing.html#assigning-values-with-indexing
The easier but obscure way of doing that is ds["time"] = ds.indexes["time"].to_datetimeindex()
. @Brian Bonnlander Can you open an issue about making this transformation easier
Can you open an issue about making this transformation easier
OK, I will. Thank you!
what's the error? I am actually using that line in a notebook now and it works.
I am having connection issues to Casper/Glade, but I will get you that error. It has to do with converting Feb. 30, I believe.
Have you tried the file in my example?
"ValueError: Cannot convert date 1950-02-29 12:00:00 to a date in the standard calendar. Reason: day is out of range for month."
OK this is a sensible error!
Yes I agree :slight_smile: I hope I can loop over years, subset the dates, convert each date to "days since Jan.1", and then use these integers as indexes into an array of dates for the Gregorian Calendar. Essentially I am trying to distribute the 5-6 missing dates in the 360-day calendar evenly over each year.
@Deepak Cherian I am still looking for any possible way to modify the cftime.Datetime360Day objects in xarray. Does this generally need to be done in a single operation for all time objects at once, or can each time object be modified individually? I have written a function that computes the replacement Gregorian date for any date on the 360-day calendar. I just can't see how to apply that function to the objects on the time axis.
ds["time"] = [your_function(t) for t in ds.time.values]
?
OK, I will give that a try. Thank you!
Thanks Deepak, your advice worked nicely. I can post code if anyone else wants to see an example of calendar conversion.
@Brian Bonnlander
A short blog post on https://ncar.github.io/xdev/ is welcome too ;)
Good idea. Are there any instructions on how to write a blog post? I think Nikola is somehow involved but I haven't done it before.
I think Nikola is somehow involved but I haven't done it before.
It's straightforward once you have nikola installed. This repo has all necessary information: https://github.com/NCAR/xdev#xdev-blog
OK, I will start writing and reply when it's ready. Thank you!
Awesome! Looking forward to it
Last updated: May 16 2025 at 17:14 UTC