Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign values to DataArray after interpolating Dataset produces an error #2854

Closed
dalonsoa opened this issue Mar 27, 2019 · 2 comments · Fixed by #2858
Closed

Assign values to DataArray after interpolating Dataset produces an error #2854

dalonsoa opened this issue Mar 27, 2019 · 2 comments · Fixed by #2858
Labels

Comments

@dalonsoa
Copy link

dalonsoa commented Mar 27, 2019

Code Sample, a copy-pastable example if possible

import numpy as np
import xarray as xr

da = xr.DataArray(
    np.random.random((3, 4)),
    dims=["assets", "year"],
    coords={"year": range(2010, 2018, 2)}
)

# Interpolate directly the DataArray
new_da = da.interp(year=[2011, 2015])
new_da.loc[{"year": 2015}] = 42    # WORKS!
print(new_da)

# Interpolate DataArray as part of a Dataset
dset = xr.Dataset({"my_array": da.copy()}).interp(year=[2011, 2015])
new_da = dset.my_array
new_da.loc[{"year": 2015}] = 42    # DOESN'T WORK!
print(new_da)

Problem description

Assigning a value to some locations of a DataArray after it has been interpolated raises an IndexError. This does not occur when the DataArray is interpolated independently, only occurs when the DataArray has been interpolated as part of a Dataset.

This problem is not present in xarray<0.12.0

Expected Output

The above example should print new_da in both cases, with the values corresponding to year 2015 replaced by 42. That works in the first case, but fails in the second raising IndexError.

Although I am not sure if it is the cause of this error, I have found that in the first case the array indexes are present and contain [2011, 2015], but they are an empty OrderDict in the second case.

Output of xr.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.7.1 (default, Nov 6 2018, 18:46:03) [Clang 10.0.0 (clang-1000.11.45.5)] python-bits: 64 OS: Darwin OS-release: 18.2.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: None LOCALE: en_GB.UTF-8 libhdf5: 1.10.2 libnetcdf: 4.4.1.1 xarray: 0.12.0 pandas: 0.23.4 numpy: 1.15.4 scipy: 1.1.0 netCDF4: 1.4.2 pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.0.3.4 nc_time_axis: None PseudonetCDF: None rasterio: None cfgrib: None iris: None bottleneck: 1.2.1 dask: None distributed: None matplotlib: 3.0.2 cartopy: None seaborn: 0.9.0 setuptools: 40.5.0 pip: 18.1 conda: None pytest: 4.0.2 IPython: 7.1.1 sphinx: None
@fmaussion
Copy link
Member

This is puzzling. I've edited the example a bit for clarity, but I have no clue what's going on:

import numpy as np
import xarray as xr

da = xr.DataArray(np.random.random((3, 4)),
                  dims=["assets", "year"],
                  coords={"year": range(2010, 2018, 2)},
                  name='var'
                  )

# Interpolate directly the DataArray
new_da = da.interp(year=[2011, 2015])
# Interpolate via dataset
new_ds = xr.Dataset({"var": da.copy()}).interp(year=[2011, 2015])['var']

# Equivalent DAs
xr.testing.assert_identical(new_da, new_ds)

# Indexing works
xr.testing.assert_identical(new_da.loc[{"year": 2015}], new_ds.loc[{"year": 2015}])

# Assignment doesn't 
new_da.loc[{"year": 2015}] = 42
new_ds.loc[{"year": 2015}] = 42  # Fails 

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-35-65ad561f1692> in <module>()
     21 # Assignment doesn't
     22 new_da.loc[{"year": 2015}] = 42
---> 23 new_ds.loc[{"year": 2015}] = 42  # Fails

....

~/Documents/git/xarray/xarray/core/indexing.py in __getitem__(self, key)
   1168     def __getitem__(self, key):
   1169         array, key = self._indexing_array_and_key(key)
-> 1170         return array[key]
   1171 
   1172     def __setitem__(self, key, value):

IndexError: index 2015 is out of bounds for axis 0 with size 2

@shoyer
Copy link
Member

shoyer commented Apr 3, 2019

It looks like this will be fixed by my latest additions to #2858.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants