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

Bar charts with NA not working when using plotly as backend #5110

Closed
Noskario opened this issue Nov 5, 2021 · 3 comments · Fixed by #5143
Closed

Bar charts with NA not working when using plotly as backend #5110

Noskario opened this issue Nov 5, 2021 · 3 comments · Fixed by #5143
Labels
type: bug Something isn't correct or isn't working
Milestone

Comments

@Noskario
Copy link

Noskario commented Nov 5, 2021

Holoviews: 1.14.6
Plotly: 5.3.1
Bokeh: 2.3.3

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

dataframe

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

Aggregated table

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)

bar-chart

I think NA's should default to zero when making Bar-charts. My original approach works fine if I use bokeh as backend.

@jbednar
Copy link
Member

jbednar commented Nov 5, 2021

I agree; there should probably be a df.fillna(0) somewhere. A suitable PR would be welcome!

@jlstevens
Copy link
Contributor

jlstevens commented Nov 8, 2021

Given that you get an exception, I'll classify this as a bug. Note that the plotly backend is marked as experimental: as Jim says, a PR would be very welcome but otherwise this is unlikely to have high priority.

@jlstevens jlstevens added the type: bug Something isn't correct or isn't working label Nov 8, 2021
@jlstevens jlstevens added this to the Post v2.0 milestone Nov 8, 2021
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants