From 6a8928fe49e24795c544be946e0d3e24e2a7fb98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8xbro=20Hansen?= Date: Mon, 11 Dec 2023 13:43:50 +0100 Subject: [PATCH] Compability updates with xarray 2023.12 --- holoviews/core/data/xarray.py | 12 +++++++++--- holoviews/tests/core/data/test_xarrayinterface.py | 12 ++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/holoviews/core/data/xarray.py b/holoviews/core/data/xarray.py index 18259f3359..823053e590 100644 --- a/holoviews/core/data/xarray.py +++ b/holoviews/core/data/xarray.py @@ -178,12 +178,18 @@ def retrieve_unit_and_label(dim): arrays[vdim.name] = arr data = xr.Dataset(arrays) else: + # Started to warn in xarray 2023.12.0: + # The return type of `Dataset.dims` will be changed to return a + # set of dimension names in future, in order to be more consistent + # with `DataArray.dims`. To access a mapping from dimension names to + # lengths, please use `Dataset.sizes`. + data_info = data.sizes if hasattr(data, "sizes") else data.dims if not data.coords: - data = data.assign_coords(**{k: range(v) for k, v in data.dims.items()}) + data = data.assign_coords(**{k: range(v) for k, v in data_info.items()}) if vdims is None: vdims = list(data.data_vars) if kdims is None: - xrdims = list(data.dims) + xrdims = list(data_info) xrcoords = list(data.coords) kdims = [name for name in data.indexes.keys() if isinstance(data[name].data, np.ndarray)] @@ -636,7 +642,7 @@ def length(cls, dataset): def dframe(cls, dataset, dimensions): import xarray as xr if cls.packed(dataset): - bands = {vd.name: dataset.data[..., i].drop('band') + bands = {vd.name: dataset.data[..., i].drop_vars('band') for i, vd in enumerate(dataset.vdims)} data = xr.Dataset(bands) else: diff --git a/holoviews/tests/core/data/test_xarrayinterface.py b/holoviews/tests/core/data/test_xarrayinterface.py index b30013da4c..fb67f917ac 100644 --- a/holoviews/tests/core/data/test_xarrayinterface.py +++ b/holoviews/tests/core/data/test_xarrayinterface.py @@ -249,8 +249,16 @@ def test_select_dropped_dimensions_restoration(self): coords=dict(chain=range(d.shape[0]), value=range(d.shape[1]))) ds = Dataset(da) t = ds.select(chain=0) - self.assertEqual(t.data.dims , dict(chain=1,value=8)) - self.assertEqual(t.data.stuff.shape , (1,8)) + if hasattr(t.data, "sizes"): + # Started to warn in xarray 2023.12.0: + # The return type of `Dataset.dims` will be changed to return a + # set of dimension names in future, in order to be more consistent + # with `DataArray.dims`. To access a mapping from dimension names to + # lengths, please use `Dataset.sizes`. + assert t.data.sizes == dict(chain=1, value=8) + else: + assert t.data.dims == dict(chain=1, value=8) + assert t.data.stuff.shape == (1, 8) def test_mask_2d_array_transposed(self): array = np.random.rand(4, 3)