holoviz / holoviz/hvplot

name assignment on hvplot.xarray.XArrayInteractive doesn't stick

Open
#1,136 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.4k
Forks
124
Avg merge
1d 18h
Merged PRs (30d)
1

Description

#### ALL software version info

```
python 3.11.5
hvplot 0.8.4
xarray 2022.12.0
```
also reproduced on
```
python 3.10.13
hvplot 0.8.4
xarray 2023.8.0
```

#### Description of expected behavior and the observed behavior

Operations on an interactive array strip away any name, which is expected behavior because the name becomes ambiguous. Assignment to the name attribute of the output of an operation is the usual next step with xarray.DataArray objects, and appears to work wih hvplot.xarray.XArrayInteractive objects. However, the name does not really seem to stick. For instance, the name should be used when converting the DataArray to a Dataset. The code below raises a `ValueError` with explanation "unable to convert unnamed DataArray to a Dataset without providing an explicit name".

#### Complete, minimal, self-contained example code that reproduces the issue

```
import xarray
import hvplot.xarray

da = xarray.DataArray([0, 1], name="a", dims="x").interactive()
db = da.dot(xarray.DataArray([1, 1], dims="x"))
db.name = "b"
db.to_dataset()
```

#### Stack traceback and/or browser JavaScript console output

```
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[3], line 7
5 db = da.dot(xarray.DataArray([1, 1], dims="x"))
6 db.name = "b"
----> 7 db.to_dataset()

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/hvplot/interactive.py:517, in Interactive.__call__(self, *args, **kwargs)
515 method = type(new._transform)(new._transform, new._method, accessor=True)
516 kwargs = dict(new._inherit_kwargs, **kwargs)
--> 517 clone = new._clone(method(*args, **kwargs), plot=new._method == 'plot')
518 finally:
519 # If an error occurs reset _method anyway so that, e.g. the next
520 # attempt in a Notebook, is set appropriately.
521 new._method = None

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/hvplot/interactive.py:388, in Interactive._clone(self, transform, plot, loc, center, dmap, copy, max_rows, **kwargs)
386 else:
387 kwargs = dict(self._inherit_kwargs, **dict(self._kwargs, **kwargs))
--> 388 return type(self)(self._obj, fn=self._fn, transform=transform, plot=plot, depth=depth,
389 loc=loc, center=center, dmap=dmap, _shared_obj=self._shared_obj,
390 max_rows=max_rows, **kwargs)

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/hvplot/interactive.py:282, in Interactive.__init__(self, obj, transform, fn, plot, depth, loc, center, dmap, inherit_kwargs, max_rows, method, _shared_obj, _current, **kwargs)
280 self._current_ = _current
281 else:
--> 282 self._current_ = self._transform.apply(ds, keep_index=True, compute=False)
283 self._init = True
284 self._dirty = False

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/holoviews/util/transform.py:774, in dim.apply(self, dataset, flat, expanded, ranges, all_values, keep_index, compute, strict)
772 drange = ranges.get(eldim, {})
773 drange = drange.get('combined', drange)
--> 774 data = self._apply_fn(dataset, data, fn, fn_name, args,
775 kwargs, accessor, drange)
776 drop_index = keep_index_for_compute and not keep_index
777 compute = not compute_for_compute and compute

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/holoviews/util/transform.py:672, in dim._apply_fn(self, dataset, data, fn, fn_name, args, kwargs, accessor, drange)
670 data = method(*args, **kwargs)
671 else:
--> 672 raise e
673 else:
674 data = fn(*args, **kwargs)

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/holoviews/util/transform.py:666, in dim._apply_fn(self, dataset, data, fn, fn_name, args, kwargs, accessor, drange)
664 else:
665 try:
--> 666 data = method(*args, **kwargs)
667 except Exception as e:
668 if 'axis' in kwargs:

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/xarray/core/dataarray.py:621, in DataArray.to_dataset(self, dim, name, promote_attrs)
619 result = self._to_dataset_split(dim)
620 else:
--> 621 result = self._to_dataset_whole(name)
623 if promote_attrs:
624 result.attrs = dict(self.attrs)

File ~/.cache/pypoetry/virtualenvs/ac-luts-ECJFV3w7-py3.11/lib/python3.11/site-packages/xarray/core/dataarray.py:565, in DataArray._to_dataset_whole(self, name, shallow_copy)
563 name = self.name
564 if name is None:
--> 565 raise ValueError(
566 "unable to convert unnamed DataArray to a "
567 "Dataset without providing an explicit name"
568 )
569 if name in self.coords:
570 raise ValueError(
571 "cannot create a Dataset from a DataArray with "
572 "the same name as one of its coordinates"
573 )

ValueError: unable to convert unnamed DataArray to a Dataset without providing an explicit name
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.