Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

resample fails with datetime #118

Open
matt-long opened this issue Apr 15, 2019 · 1 comment
Open

resample fails with datetime #118

matt-long opened this issue Apr 15, 2019 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@matt-long
Copy link
Contributor

I am having trouble with esmlab resample. I am working with monthly data with standard calendar.

Here are a two failure modes.

  1. 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')
  1. 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.

@andersy005
Copy link
Contributor

Partially fixed by #122

@andersy005 andersy005 removed this from the sprint-apr1-apr26 milestone Apr 27, 2019
@andersy005 andersy005 added the core label May 6, 2019
@andersy005 andersy005 added bug Something isn't working and removed bug labels Jul 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants