Skip to content

Commit

Permalink
Assign int index of coords if no coords in xarray object (#2968)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsignell authored and philippjfr committed Aug 30, 2018
1 parent d38f11c commit 39bdc02
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions holoviews/core/data/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def retrieve_unit_and_label(dim):
arrays[vdim.name] = arr
data = xr.Dataset(arrays)
else:
if not data.coords:
data = data.assign_coords(**{k: range(v) for k, v in data.dims.items()})
if vdims is None:
vdims = list(data.data_vars.keys())
vdims = [retrieve_unit_and_label(vd) for vd in vdims]
Expand Down
26 changes: 25 additions & 1 deletion tests/core/data/testxarrayinterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def init_grid_data(self):
self.dataset_grid_inv = self.element((self.grid_xs[::-1], self.grid_ys[::-1],
dask_zs), kdims=['x', 'y'],
vdims=['z'])

def test_xarray_dataset_with_scalar_dim_canonicalize(self):
import dask.array
xs = [0, 1]
Expand Down Expand Up @@ -262,6 +262,30 @@ def test_select_on_transposed_dataarray(self):
img = Image(array)[1:3]
self.assertEqual(img['z'], Image(array.sel(x=slice(1, 3)))['z'])

def test_dataarray_with_no_coords(self):
expected_xs = list(range(2))
expected_ys = list(range(3))
zs = np.arange(6).reshape(2, 3)
xrarr = xr.DataArray(zs, dims=('x','y'))

img = Image(xrarr)
self.assertTrue(all(img.data.x == expected_xs))
self.assertTrue(all(img.data.y == expected_ys))

img = Image(xrarr, kdims=['x', 'y'])
self.assertTrue(all(img.data.x == expected_xs))
self.assertTrue(all(img.data.y == expected_ys))

def test_dataarray_with_some_coords(self):
xs = [4.2, 1]
zs = np.arange(6).reshape(2, 3)
xrarr = xr.DataArray(zs, dims=('x','y'), coords={'x': xs})

with self.assertRaises(ValueError):
Image(xrarr)

with self.assertRaises(ValueError):
Image(xrarr, kdims=['x', 'y'])


@attr(optional=1)
Expand Down

0 comments on commit 39bdc02

Please sign in to comment.