From 77f80dea4e517ccab1a0780f385f0bca35fed2c6 Mon Sep 17 00:00:00 2001 From: Jerry Vinokurov Date: Mon, 22 Jul 2024 00:31:13 -0400 Subject: [PATCH 1/2] fix a problem where linked selections were resulting in repeated columns --- holoviews/element/selection.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/holoviews/element/selection.py b/holoviews/element/selection.py index a6c8ff43ac..349c3ee667 100644 --- a/holoviews/element/selection.py +++ b/holoviews/element/selection.py @@ -32,10 +32,12 @@ def _get_index_selection(self, index, index_cols): self._index_skip = True if not index: return None, None, None - ds = self.clone(kdims=index_cols, new_type=Dataset) + clone_vdims = [vdim.name for vdim in self.vdims if vdim.name not in index_cols] + cols = clone_vdims + index_cols + ds = self.clone(kdims=index_cols, vdims=clone_vdims, new_type=Dataset) if len(index_cols) == 1: index_dim = index_cols[0] - vals = dim(index_dim).apply(ds.iloc[index], expanded=False) + vals = dim(index_dim).apply(ds.iloc[index, cols], expanded=False) if vals.dtype.kind == 'O' and all(isinstance(v, np.ndarray) for v in vals): vals = [v for arr in vals for v in util.unique_iterator(arr)] expr = dim(index_dim).isin(list(util.unique_iterator(vals))) @@ -43,7 +45,7 @@ def _get_index_selection(self, index, index_cols): get_shape = dim(self.dataset.get_dimension(index_cols[0]), np.shape) index_cols = [dim(self.dataset.get_dimension(c), np.ravel) for c in index_cols] vals = dim(index_cols[0], util.unique_zip, *index_cols[1:]).apply( - ds.iloc[index], expanded=True, flat=True + ds.iloc[index, cols], expanded=True, flat=True ) contains = dim(index_cols[0], util.lzip, *index_cols[1:]).isin(vals, object=True) expr = dim(contains, np.reshape, get_shape) From fd09e2fa9fbd717dcaaad3bf948d5b136ac6bc9b Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Wed, 16 Oct 2024 13:59:20 +0200 Subject: [PATCH 2/2] Add test --- holoviews/tests/element/test_selection.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/holoviews/tests/element/test_selection.py b/holoviews/tests/element/test_selection.py index 6c2e891019..05f8ff3418 100644 --- a/holoviews/tests/element/test_selection.py +++ b/holoviews/tests/element/test_selection.py @@ -30,6 +30,7 @@ ) from holoviews.element.comparison import ComparisonTestCase from holoviews.element.selection import spatial_select_columnar +from holoviews.util.transform import dim from ..utils import dask_switcher @@ -61,6 +62,29 @@ dd_available = pytest.mark.skipif(dd is None, reason='dask.dataframe not available') +class TestIndexExpr(ComparisonTestCase): + + def setUp(self): + import holoviews.plotting.bokeh # noqa + super().setUp() + self._backend = Store.current_backend + Store.set_current_backend('bokeh') + + def tearDown(self): + Store.current_backend = self._backend + + def test_index_selection_on_id_column(self): + # tests issue in https://github.com/holoviz/holoviews/pull/6336 + x, y = np.random.randn(2, 100) + idx = np.arange(100) + + points = Points( + {'x': x, 'y': y, 'id': idx}, kdims=['x', 'y'], vdims=['id'], datatype=['dataframe'] + ) + sel, _, _ = points._get_index_selection([3, 7], ['id']) + assert sel == dim('id').isin([3, 7]) + + class TestSelection1DExpr(ComparisonTestCase): def setUp(self):