I want to create a bar-chart with the plotly backend. I use the aggregate-function to count the number of items in each category. Let me show an example.
First I create some test-data:
import holoviews as hv
hv.extension('plotly')
import numpy as np
import pandas as pd
samples = 100
pets = ['Cat', 'Dog', 'Hamster', 'Rabbit']
genders = ['Female', 'Male']
pets_sample = np.random.choice(pets, samples)
gender_sample = np.random.choice(genders, samples)
df=pd.DataFrame(data={'pet':pets_sample,'gender':gender_sample,})
df['pet']=pd.Categorical(df['pet'])
df['gender']=pd.Categorical(df['gender'])
# Delete male hamsters so we have an empty category-combination
df=df[~((df['pet']=='Hamster') & (df['gender']=='Male'))]
df['name']=['Animal #'+str(i) for i in range(len(df))]
df=df[['name','pet','gender']]
df
When I try to plot this, using
bars = hv.Bars(df, kdims=['pet', 'gender'],vdims='name').aggregate(function=np.count_nonzero)
bars.opts(width=1000,multi_level=False)
I get the following error: ValueError: Out of range float values are not JSON compliant.
The reason for that is (I think) that there is one NA in the aggregated table:
hv.Table(df, kdims=['pet', 'gender'],vdims='name').aggregate(function=np.count_nonzero)
returns
You can solve this problem by adding a new column to the dataframe, just consisting of ones and use np.sum instead of np.count_nonzero and then everything works:
df['ones']=1
bars=hv.Bars(df, kdims=['pet', 'gender'],vdims=[('ones','count')]).aggregate(function=np.sum)
bars.opts(width=1000,multi_level=False)
I think NA's should default to zero when making Bar-charts. My original approach works fine if I use bokeh as backend.
I would like to know where would be a good place to address this issue. The github of holoviews of plotly?


