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

Transform columnar data for interactive #1042

Merged
merged 5 commits into from
Mar 16, 2023
Merged

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Mar 15, 2023

In the next release of Holoviews 1.16, this will raise an error:

import hvplot.pandas
import pandas as pd

pd.DataFrame([1, 2, 3, 4]).interactive()

Fixes a known issue first shown here #918 and are related to #932.

Other places that use hv.Dataset are here. I don't know if they will error:
image

@maximlt
Copy link
Member

maximlt commented Mar 16, 2023

Thanks a lot for reminding us of this, that was completely out of my radar!

I refactored a bit the code and added a test.

About the remaining occurrences of hv.Dataset:

  • Scatter Matrix: applied the conversion as it's easy to create a scatter matrix from pd.DataFrame(np.random.random(...)), just to try it out
  • Parallel coordinates: did not apply the conversion, didn't really see how one could end up with non string columns that can be used in this context
  • Andrew curves: same

@hoxbro
Copy link
Member Author

hoxbro commented Mar 16, 2023

Love the changes.

A small edge case could be a xarray.DataArray (or similar) with a variable named columns.

import hvplot.xarray
import xarray as xr

xr.DataArray([1, 2, 3, 4], dims=["columns"], name="test").interactive()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[12], line 4
      1 import hvplot.xarray
      2 import xarray as xr
----> 4 xr.DataArray([1, 2, 3, 4], dims=["columns"], name="test").interactive()

File ~/miniconda3/envs/holoviz/lib/python3.10/site-packages/xarray/core/extensions.py:38, in _CachedAccessor.__get__(self, obj, cls)
     35     pass
     37 try:
---> 38     accessor_obj = self._accessor(obj)
     39 except AttributeError:
     40     # __getattr__ on data object will swallow any AttributeErrors
     41     # raised when initializing the accessor, so we need to raise as
     42     # something else (GH933):
     43     raise RuntimeError(f"error initializing {self._name!r} accessor.")

File ~/Repos/holoviz/hvplot/hvplot/interactive.py:278, in Interactive.__init__(self, obj, transform, fn, plot, depth, loc, center, dmap, inherit_kwargs, max_rows, method, _shared_obj, _current, **kwargs)
    276 self._max_rows = max_rows
    277 self._kwargs = kwargs
--> 278 ds = hv.Dataset(_convert_col_names_to_str(self._obj))
    279 if _current is not None:
    280     self._current_ = _current

File ~/Repos/holoviz/hvplot/hvplot/util.py:558, in _convert_col_names_to_str(data)
    556 if not hasattr(data, 'columns'):
    557     return data
--> 558 renamed = {c: str(c) for c in data.columns if not isinstance(c, str)}
    559 if renamed:
    560     data = data.rename(columns=renamed)

File ~/Repos/holoviz/hvplot/hvplot/util.py:558, in <dictcomp>(.0)
    556 if not hasattr(data, 'columns'):
    557     return data
--> 558 renamed = {c: str(c) for c in data.columns if not isinstance(c, str)}
    559 if renamed:
    560     data = data.rename(columns=renamed)

TypeError: unhashable type: 'DataArray'

For this case it seems to be possible to add a hashable check:

from collections.abc import Hashable

{c: str(c) for c in list(data.columns) if not isinstance(c, str) and isinstance(c, Hashable)}

@maximlt
Copy link
Member

maximlt commented Mar 16, 2023

Last suggestion by @hoxbro added in 4093310.

Unrelated job failure, merging.

@maximlt maximlt merged commit 4e9e59b into main Mar 16, 2023
@maximlt maximlt deleted the interactive_int_columns branch March 16, 2023 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants