From 7d9e130782f75c59121d52c6ab1f95658a5ac205 Mon Sep 17 00:00:00 2001 From: Julia Signell Date: Thu, 30 Aug 2018 16:16:28 -0400 Subject: [PATCH] Assign int index of coords if no coords in xarray object (#2968) --- holoviews/core/data/xarray.py | 2 ++ tests/core/data/testxarrayinterface.py | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/holoviews/core/data/xarray.py b/holoviews/core/data/xarray.py index 4f3bea3ca1..f9d5442066 100644 --- a/holoviews/core/data/xarray.py +++ b/holoviews/core/data/xarray.py @@ -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] diff --git a/tests/core/data/testxarrayinterface.py b/tests/core/data/testxarrayinterface.py index 05e5310d81..49959b91ea 100644 --- a/tests/core/data/testxarrayinterface.py +++ b/tests/core/data/testxarrayinterface.py @@ -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] @@ -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)