8

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?

4
  • 1
    The Interval object is not JSON serializable - i.e. JSON lib doesn't know how to convert it to JSON format. You would need to implement a method for encoding the Interval on your side and decoding it on plotly back to interval or some adequate representation. A workaround might be to list the interval with enough granularity i.e. (1,100] to [1.0001, 1.01, 2, 3, ..., 100] which should be OK with json. Commented Oct 23, 2019 at 5:44
  • By the way, what library do you use, that it allows you to write the interval like (1, 2]? Commented Oct 23, 2019 at 5:53
  • 1
    I'm using pd.qcut to divide it into four groups Commented Oct 23, 2019 at 6:05
  • @MarekSchwarz example code please. Commented Dec 16, 2020 at 7:32

1 Answer 1

17

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.