Stream: python-questions

Topic: Modifying time objects in xarray


view this post on Zulip Brian Bonnlander (Jul 17 2020 at 21:13):

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

view this post on Zulip Brian Bonnlander (Jul 17 2020 at 21:23):

Note that I'm ultimately trying to convert all elements to datetime64 objects. And modify their 'dayofyear' values in the process.

view this post on Zulip Matt Long (Jul 17 2020 at 22:00):

You should not be using isel for assignment:
http://xarray.pydata.org/en/stable/indexing.html#assigning-values-with-indexing

view this post on Zulip Deepak Cherian (Jul 17 2020 at 23:30):

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

view this post on Zulip Brian Bonnlander (Jul 18 2020 at 04:30):

Can you open an issue about making this transformation easier

OK, I will. Thank you!

view this post on Zulip Deepak Cherian (Jul 20 2020 at 20:44):

what's the error? I am actually using that line in a notebook now and it works.

view this post on Zulip Brian Bonnlander (Jul 20 2020 at 20:45):

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.

view this post on Zulip Brian Bonnlander (Jul 20 2020 at 20:46):

Have you tried the file in my example?

view this post on Zulip Deepak Cherian (Jul 20 2020 at 20:49):

"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!

view this post on Zulip Brian Bonnlander (Jul 20 2020 at 20:55):

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.

view this post on Zulip Brian Bonnlander (Jul 21 2020 at 23:40):

@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.

view this post on Zulip Deepak Cherian (Jul 21 2020 at 23:42):

ds["time"] = [your_function(t) for t in ds.time.values]?

view this post on Zulip Brian Bonnlander (Jul 21 2020 at 23:43):

OK, I will give that a try. Thank you!

view this post on Zulip Brian Bonnlander (Jul 22 2020 at 21:32):

Thanks Deepak, your advice worked nicely. I can post code if anyone else wants to see an example of calendar conversion.

view this post on Zulip Anderson Banihirwe (Jul 22 2020 at 21:36):

@Brian Bonnlander

A short blog post on https://ncar.github.io/xdev/ is welcome too ;)

view this post on Zulip Brian Bonnlander (Jul 22 2020 at 21:37):

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.

view this post on Zulip Anderson Banihirwe (Jul 22 2020 at 21:38):

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

view this post on Zulip Brian Bonnlander (Jul 22 2020 at 21:45):

OK, I will start writing and reply when it's ready. Thank you!

view this post on Zulip Anderson Banihirwe (Jul 22 2020 at 21:55):

Awesome! Looking forward to it


Last updated: Jan 30 2022 at 12:01 UTC