Search code examples
pythonpython-3.xholoviews

holoviews dataset select dimension with illegal python variable characters


I have a holoviews dataset where the name of one of the key dimensions have a space and parenthesis in it. How can I do the equivalent of ds.select(dimension_name=dimension_value) for this dimension? Please see example below.

import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)

data = {
    'a (a)': [1, 1, 2, 2],
    'b': [1, 2, 3, 4],
}

df_ = pd.DataFrame(data=data)

ds_ = hv.Dataset(df_, kdims=['a (a)'], vdims=['b'])

ds_.select(WHAT DO I PUT HERE=1)['b'] # <-- What to do?


Solution

  • Normally you would just select like this:

    ds_.select(a=1)
    


    But since this column name is an illegal python variable name, you have to pass a dictionary that will be unpacked by using double stars:

    ds_.select(**{'a (a)': 1})
    


    Slicing with this illegal variable name can also be done using a dictionary:

    ds_.select(**{'a (a)': (1, 2.5)})
    


    Or, alternatively, if you don't mind changing your dataset:
    You could rename your column, replace the space and parentheses etc. in your pandas dataframe. Or do the selection on the pandas dataframe: df_[df_['a (a)'] == 1]


    Your similar question on discourse.holoviz.org resulted in the correct answer, which also made me learn again :)
    https://discourse.holoviz.org/t/how-to-change-order-of-data-of-points-with-non-numeric-x-axis/52/5


    More info on selecting data in holoviews:
    http://holoviews.org/user_guide/Indexing_and_Selecting_Data.html