From 56727bed04e461929b363010f609e8abb3c5a850 Mon Sep 17 00:00:00 2001 From: maximlt Date: Sun, 8 Dec 2024 18:46:52 +0100 Subject: [PATCH 1/4] return early when no data --- holoviews/core/data/spatialpandas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/holoviews/core/data/spatialpandas.py b/holoviews/core/data/spatialpandas.py index 66d3e621c2..c1ef977baa 100644 --- a/holoviews/core/data/spatialpandas.py +++ b/holoviews/core/data/spatialpandas.py @@ -616,6 +616,8 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, Returns: An array containing the values along a dimension """ + if not len(data): + return np.array([]) column = data[dimension.name] if keep_index: return column @@ -639,8 +641,6 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, if expanded and not is_points and not i == (len(data[geom_col])-1): arrays.append(np.array([np.nan])) - if not len(data): - return np.array([]) if expanded: return np.concatenate(arrays) if len(arrays) > 1 else arrays[0] elif (all_scalar and arrays): From fe122ca02f44394c106158d32907bb1e479919e9 Mon Sep 17 00:00:00 2001 From: maximlt Date: Sun, 8 Dec 2024 18:47:56 +0100 Subject: [PATCH 2/4] compute geom lenght only when needed --- holoviews/core/data/spatialpandas.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/holoviews/core/data/spatialpandas.py b/holoviews/core/data/spatialpandas.py index c1ef977baa..ecb509ab87 100644 --- a/holoviews/core/data/spatialpandas.py +++ b/holoviews/core/data/spatialpandas.py @@ -624,7 +624,6 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, all_scalar = True arrays, scalars = [], [] for i, geom in enumerate(data[geom_col]): - length = 1 if is_points else geom_length(geom) val = column.iloc[i] scalar = isscalar(val) if scalar: @@ -637,6 +636,7 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, if not expanded or not scalar: arrays.append(val) elif scalar: + length = 1 if is_points else geom_length(geom) arrays.append(np.full(length, val)) if expanded and not is_points and not i == (len(data[geom_col])-1): arrays.append(np.array([np.nan])) From 3fc8935ebbb9772bf5c68296c0b02d9ad51c6f96 Mon Sep 17 00:00:00 2001 From: maximlt Date: Sun, 8 Dec 2024 18:52:22 +0100 Subject: [PATCH 3/4] compute unique scalar values early --- holoviews/core/data/spatialpandas.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/holoviews/core/data/spatialpandas.py b/holoviews/core/data/spatialpandas.py index ecb509ab87..510846cc38 100644 --- a/holoviews/core/data/spatialpandas.py +++ b/holoviews/core/data/spatialpandas.py @@ -621,17 +621,19 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, column = data[dimension.name] if keep_index: return column - all_scalar = True + is_scalars = [isscalar(value) for value in column] + all_scalar = all(is_scalars) + if all_scalar and not expanded: + return column.unique() arrays, scalars = [], [] for i, geom in enumerate(data[geom_col]): val = column.iloc[i] - scalar = isscalar(val) + scalar = is_scalars[i] if scalar: val = np.array([val]) if not scalar and len(unique_array(val)) == 1: val = val[:1] scalar = True - all_scalar &= scalar scalars.append(scalar) if not expanded or not scalar: arrays.append(val) From c3c8f4437d78f49761c60ef7f62a6936f1e73b2a Mon Sep 17 00:00:00 2001 From: maximlt Date: Sun, 8 Dec 2024 19:37:11 +0100 Subject: [PATCH 4/4] handle calling unique on a categorical column --- holoviews/core/data/spatialpandas.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/holoviews/core/data/spatialpandas.py b/holoviews/core/data/spatialpandas.py index 510846cc38..5a8af8b8cd 100644 --- a/holoviews/core/data/spatialpandas.py +++ b/holoviews/core/data/spatialpandas.py @@ -624,7 +624,9 @@ def get_value_array(data, dimension, expanded, keep_index, geom_col, is_scalars = [isscalar(value) for value in column] all_scalar = all(is_scalars) if all_scalar and not expanded: - return column.unique() + scal_unique = column.unique() + # .unique() on categorical dtype doesn't return a np array + return scal_unique if isinstance(scal_unique, np.ndarray) else scal_unique.to_numpy() arrays, scalars = [], [] for i, geom in enumerate(data[geom_col]): val = column.iloc[i]