Stream: python-questions
Topic: time slice different calendars error
Danica Lombardozzi (Jul 02 2021 at 21:21):
I'm trying to extract a subset of years for a variable using the tried-and-true .sel
method. I've used the same code block in a different script without any problems, but I'm getting an error message:
TypeError: cannot compare cftime.datetime(1992, 7, 16, 12, 0, 0, 0, calendar='noleap') and cftime.datetime(1950, 1, 1, 0, 0, 0, 0, calendar='gregorian') (different calendars)
The error message corresponds to the last line of this code:
dat = xr.open_dataset('/glade/p/cesm/lmwg_dev/dll/CESM2_Coupled_NoCrop/b.e21.BHIST_BPRP.f09_g17.CMIP6-esm-hist.001.cam.h0.CO2.185001-201412.nc') CESM2ppm = dat.CO2 CESM2ppm.sel(time=slice('1950','2014'))
The time array is noleap
, and I'm not sure why using either sel
or slice
is comparing to a gregorian calendar.
time (time) object 1850-01-16 12:00:00 ... 2014-12-... array([cftime.datetime(1850, 1, 16, 12, 0, 0, 0, calendar='noleap'), cftime.datetime(1850, 2, 15, 0, 0, 0, 0, calendar='noleap'), cftime.datetime(1850, 3, 16, 12, 0, 0, 0, calendar='noleap'), ..., cftime.datetime(2014, 10, 16, 12, 0, 0, 0, calendar='noleap'), cftime.datetime(2014, 11, 16, 0, 0, 0, 0, calendar='noleap'), cftime.datetime(2014, 12, 16, 12, 0, 0, 0, calendar='noleap')], dtype=object)
Any ideas what the problem is or how I can work around this? Thanks in advance!
Katie Dagon (Jul 02 2021 at 21:48):
Hi Danica! I tried the same code in a notebook and it worked for me. Could it be your version of xarray or something else in your environment? For example, I'm wondering if you need to have cf-xarray installed -- sometimes that does the trick for me.
Danica Lombardozzi (Jul 02 2021 at 23:20):
Thanks Katie! Adding cf-xarray didn't do the trick (or maybe I don't know how to install it properly -- I'm still learning how to manage environments), but I switched to a different environment and it worked. I'll have to see if I can figure out what's different about the environments that is making the difference.
Deepak Cherian (Jul 02 2021 at 23:21):
it's probably the cftime
version.
Danica Lombardozzi (Jul 02 2021 at 23:25):
Thanks! I'll give that a try next!
Amanda Fay (Oct 01 2021 at 20:21):
@Matt Long Maybe some tips here with your cftime slice issue?
Matt Long (Oct 01 2021 at 23:00):
Thanks @Amanda Fay for pointing me here. I encountered what appears to be the same issue and documented it here:
https://zulip2.cloud.ucar.edu/#narrow/stream/10-python-questions/topic/datetime.20index/near/44187
This seems like a bug, though perhaps there is a cftime
version works.
Matt Long (Oct 01 2021 at 23:00):
note that I have a work-around
Last updated: Jan 30 2022 at 12:01 UTC