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

Allow to sort the values of the dimensions used in a groupby operation #6471

Open
maximlt opened this issue Dec 8, 2024 · 3 comments
Open
Labels
tag: hvplot Issue also been asked in hvplot type: enhancement Minor feature or improvement to an existing feature

Comments

@maximlt
Copy link
Member

maximlt commented Dec 8, 2024

Noticed while modernizing NYC Buildings (holoviz-topics/examples#386), it would be nice to be able to declare whether to sort or not the values of the dimensions used in a groupby operation.

import holoviews as hv
import pandas as pd

hv.extension('bokeh')

N = 12
df = pd.DataFrame({
    'cat1': ['A', 'A', 'A', 'A', 'C', 'C', 'C', 'C', 'B', 'B', 'B', 'B'],
    'cat2': ['y', 'y', 'x', 'x', 'y', 'y', 'x', 'x','y', 'y', 'x', 'x'],
    'x'  : range(N),
    'y'  : range(N),
})

hv.Points(df, ['x', 'y'], ['cat1', 'cat2']).groupby(['cat1', 'cat2'], dynamic=True)

Order is ACB, I'd like to sort to ABC.

Image

@maximlt maximlt added tag: hvplot Issue also been asked in hvplot type: enhancement Minor feature or improvement to an existing feature labels Dec 8, 2024
@maximlt
Copy link
Member Author

maximlt commented Dec 8, 2024

Ideally, this would need to be supported for both dynamic=True and dynamic=False. For the latter, in practice it seems that the values get sorted in most cases as HoloMap (the groupby container), like others, has sort=True by default.

Supporting sorting specific dimensions only is probably out of scope, at least that's not what I'm requesting here :)

@hoxbro
Copy link
Member

hoxbro commented Dec 9, 2024

I would not be opposed to have these just sorted by default. Though, this need to be updated in Panel, example of code:

diff --git a/panel/pane/holoviews.py b/panel/pane/holoviews.py
index a10fae643..e3a10aaa5 100644
--- a/panel/pane/holoviews.py
+++ b/panel/pane/holoviews.py
@@ -635,7 +635,11 @@ class HoloViews(Pane):
 
             vals = dim.values or values.get(dim, None)
             if vals is not None:
                 vals = list(unique_iterator(vals))
+                with suppress(Exception):
+                    vals = sorted(vals)
             dim_values[dim.name] = vals
             if widgets_type == 'scrubber':
                 if not vals:

@maximlt
Copy link
Member Author

maximlt commented Dec 10, 2024

Though, this need to be updated in Panel, example of code:

Hm that'd work but I'm not sure how HoloViews would then let users toggle the sorting behavior.

When I looked at it, I thought about updating Dataset.groupby with a new kwarg (e.g. sort_dim_values). For dynamic=True this would be here:

dynamic_dims = [d.clone(values=list(self.interface.values(self, d.name, False)))
for d in dimensions]

I didn't look much into the dynamic=False case. The dim values aren't obtained in Dataset.groupby but within each interface's groupby method. So sort_dim_values would need to percolate down these methods and be handled there. Note that the group container types often (always?) have a sort parameter (inherited from MultiDimensionalMapping, True by default) so there's possibly a way to leverage that. Final note, some groupby methods already have some code related to sorting, for instance Pandas' interface (for performance reasons?):

groupby_kwargs = {"sort": False}
if PANDAS_GE_2_1_0:
groupby_kwargs["observed"] = False
data = [(k, group_type(v, **group_kwargs)) for k, v in
dataset.data.groupby(group_by, **groupby_kwargs)]
if issubclass(container_type, NdMapping):
with item_check(False), sorted_context(False):
return container_type(data, kdims=index_dims)
else:
return container_type(data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tag: hvplot Issue also been asked in hvplot type: enhancement Minor feature or improvement to an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants