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

esmlab.resample() error with xarray 0.15.1 #165

Open
mnlevy1981 opened this issue Apr 1, 2020 · 1 comment
Open

esmlab.resample() error with xarray 0.15.1 #165

mnlevy1981 opened this issue Apr 1, 2020 · 1 comment
Labels
bug Something isn't working

Comments

@mnlevy1981
Copy link
Contributor

mnlevy1981 commented Apr 1, 2020

Description

I have a notebook that relies on esmlab.resample() that dies with ValueError: Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable 'time'.

What I Did

Cell [6] from this notebook:

%%time

datasets_ann = dict()
for exp in datasets:
    datasets_ann[exp] = esmlab.resample(datasets[exp], freq='ann')
    print(f'\n{exp}\n----\n{datasets_ann[exp]}')

resulted in

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<timed exec> in <module>

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/esmlab/core.py in resample(dset, freq, weights, time_coord_name, method)
    780 
    781     else:
--> 782         ds = dset.esmlab.set_time(time_coord_name=time_coord_name).compute_ann_mean(
    783             weights=weights, method=method
    784         )

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/esmlab/core.py in set_time(self, time_coord_name, year_offset)
    358                     print('Please open dataset with `decode_times=False`')
    359                     raise exc
--> 360         self.setup()
    361         return self
    362 

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/esmlab/core.py in setup(self)
    363     def setup(self):
    364         self.get_variables()
--> 365         self.compute_time()
    366         self.get_original_metadata()
    367 

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/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:

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/esmlab/core.py in get_time_decoded(self, midpoint)
    185                 units=self.time_attrs['units'],
    186                 calendar=self.time_attrs['calendar'],
--> 187                 only_use_cftime_datetimes=True,
    188             )
    189         )

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/xarray/core/common.py in __setattr__(self, name, value)
    260         """
    261         try:
--> 262             object.__setattr__(self, name, value)
    263         except AttributeError as e:
    264             # Don't accidentally shadow custom AttributeErrors, e.g.

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/xarray/core/dataarray.py in data(self, value)
    551     @data.setter
    552     def data(self, value: Any) -> None:
--> 553         self.variable.data = value
    554 
    555     @property

/glade/work/mlevy/miniconda3/envs/cesm2-marbl-new/lib/python3.7/site-packages/xarray/core/variable.py in data(self, data)
   2106     def data(self, data):
   2107         raise ValueError(
-> 2108             f"Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable {self.name!r}. "
   2109             f"Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate."
   2110         )

ValueError: Cannot assign to the .data attribute of dimension coordinate a.k.a IndexVariable 'time'. Please use DataArray.assign_coords, Dataset.assign_coords or Dataset.assign as appropriate.

Output of esmlab.show_versions()

>>> esmlab.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.7.6 | packaged by conda-forge | (default, Mar 23 2020, 23:03:20)
[GCC 7.3.0]
python-bits: 64
OS: Linux
OS-release: 3.10.0-693.21.1.el7.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

esmlab: 2019.4.27.post55
xarray: 0.15.1
pandas: 1.0.3
numpy: 1.18.1
scipy: 1.4.1
xesmf: installed
cftime: 1.0.3.4
dask: 2.13.0
distributed: 2.13.0
setuptools: 46.1.3.post20200325
pip: 20.0.2
conda: None
pytest: 5.4.1
IPython: 7.13.0
sphinx: None
@mnlevy1981 mnlevy1981 added the bug Something isn't working label Apr 1, 2020
@mnlevy1981
Copy link
Contributor Author

Sorry, my fingers slipped and I managed to use keyboard shortcuts to hit "submit" before I was ready... but I updated the issue to finish including requested info.

I think the fix is to change line 200 in core.py to something like

        time = time.assign_coords(cftime.date2num(
            time, units=self.time_attrs['units'], calendar=self.time_attrs['calendar']
        ))

(The syntax may be off by a little bit, I've been using Dataset.assign_coords rather than DataArray.assign_coords to fix similar errors in my notebooks). I'll play with it tomorrow and report back.

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

1 participant