Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object of type Interval is not JSON serializable in Plotly, Python

I have converted a continuous variable, x, into interval. And i have a y variable with numerical values. The dataframe is:

data = {'x':[(-0.001, 7.0], (7.0, 19.0], (19.0, 97.0], (97.0, 817.0]],
        'y':[769.0, 810.0,757.0,652.0]}

# Create DataFrame      
df = pd.DataFrame(data)  
df 

The data types for both these variables are float64. Furthermore, the description for variable 'x' is given as:

Name: x, dtype: category
Categories (4, interval[float64]): [(-0.001, 7.0] < (7.0, 19.0] < (19.0, 97.0] < (97.0, 817.0]]

Now, i'm using plotly to graph the relationship between these two variables:

# figure
plot_data = [
    go.Scatter(
        x = df['x'],
        y = df['y'])]

plot_layout = go.Layout(title=' Relationship between x and y')               
fig = go.Figure(data=plot_data, layout=plot_layout)
pyoff.iplot(fig)

But the error shown is:

TypeError: Object of type Interval is not JSON serializable

So, as far as I understood, the variable 'x' is given as interval in a format which plotly is unable to identify. How to fix this? Is there any example known to you where one can use plotly to plot interval variable?

like image 282
sayan de sarkar Avatar asked Jun 10 '26 21:06

sayan de sarkar


1 Answers

you only need to cast the column with ".astype('str')"

like image 188
Mayra Alejandra Sanchez Avatar answered Jun 13 '26 13:06

Mayra Alejandra Sanchez