You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
I am having trouble with esmlab resample. I am working with monthly data with standard calendar.
Here are a two failure modes.
Read dataset with decode_times=True
In [1]: ds=xr.open_dataset(file_in, drop_variables='date', decode_times=True)
In [2]: ds_ann=esmlab.resample(ds, freq='ann')
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-15-0640091b3d29> in <module>
----> 1 ds_ann = esmlab.resample(ds, freq='ann')
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in resample(dset, freq, weights, time_coord_name)
723
724 else:
--> 725 ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_ann_mean(weights=weights)
726
727 new_history = f'\n{datetime.now()} esmlab.resample(<DATASET>, freq="{freq}")'
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in set_time(self, time_coord_name, year_offset)
319 self._ds[self.tb_name],
320 units=self.time_attrs['units'],--> 321 calendar=self.time_attrs['calendar'],
322 )
323 self.time_bound.data = tb_data
cftime/_cftime.pyx in cftime._cftime.date2num()
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')
Read dataset with decode_times=False
In [1]: file_in='/glade/work/mclong/pco2-landschutzer/downloaded/MPI-SOM_FFN_GCB2018.nc'In [2]: ds=xr.open_dataset(file_in, drop_variables='date', decode_times=False)
In [3]: ds_ann=esmlab.resample(ds, freq='ann')
Error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-11-0640091b3d29> in <module>
----> 1 ds_ann = esmlab.resample(ds, freq='ann')
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in resample(dset, freq, weights, time_coord_name)
723
724 else:
--> 725 ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_ann_mean(weights=weights)
726
727 new_history = f'\n{datetime.now()} esmlab.resample(<DATASET>, freq="{freq}")'
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in set_time(self, time_coord_name, year_offset)
322 )
323 self.time_bound.data = tb_data
--> 324 self.setup()
325 return self
326
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in setup(self)
327 def setup(self):
328 self.get_variables()
--> 329 self.compute_time()
330 self.get_original_metadata()
331
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in compute_time(self)
80
81 if self.time_bound is not None:
---> 82 groupby_coord = self.get_time_decoded(midpoint=True)
83
84 else:
/gpfs/u/home/mclong/codes/esmlab/esmlab/core.py in get_time_decoded(self, midpoint)
181 time_out.data = xr.CFTimeIndex(
182 cftime.num2date(
--> 183 time_data, units=self.time_attrs['units'], calendar=self.time_attrs['calendar']
184 )
185 )
/glade/work/mclong/miniconda3/envs/dev/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in __new__(cls, data, name)
235 result = object.__new__(cls)
236 result._data = np.array(data, dtype='O')
--> 237 assert_all_valid_date_type(result._data)
238 result.name = name
239 return result
/glade/work/mclong/miniconda3/envs/dev/lib/python3.7/site-packages/xarray/coding/cftimeindex.py in assert_all_valid_date_type(data)
191 raise TypeError(
192 'CFTimeIndex requires cftime.datetime '
--> 193 'objects. Got object of {}.'.format(date_type))
194 if not all(isinstance(value, date_type) for value in data):
195 raise TypeError(
TypeError: CFTimeIndex requires cftime.datetime objects. Got object of <class 'cftime._cftime.real_datetime'>.
Workaround:
In [1]: ds=xr.open_dataset(file_in, drop_variables='date', decode_times=False)
In [2]: ds['time'] =cftime.num2date(ds.time, units=ds.time.units,
...: only_use_cftime_datetimes=True)
From cftime documentation: only_use_cftime_datetimes: if False (default), datetime.datetime objects are returned from num2date where possible; if True dates which subclass cftime.datetime are returned for all calendars.
Conclusion: esmlab is not datetime friendly.
The text was updated successfully, but these errors were encountered:
I am having trouble with
esmlab
resample. I am working with monthly data with standard calendar.Here are a two failure modes.
decode_times=True
Error:
decode_times=False
Error:
Workaround:
From
cftime
documentation:only_use_cftime_datetimes
: if False (default), datetime.datetime objects are returned from num2date where possible; if True dates which subclass cftime.datetime are returned for all calendars.Conclusion: esmlab is not
datetime
friendly.The text was updated successfully, but these errors were encountered: