Skip to content

Cannot plot a plotly chart from pandas dataframe with DateTimeIndex #517

Closed Answered by falkoschindler
Nikodemski2002 asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, I boiled it down to this:

from io import StringIO

import matplotlib.pyplot as plt
import pandas as pd
from plotly import graph_objects as go

from nicegui import ui

csv = '''Date;Value
01.01.2018 00:00;100.0
01.01.2018 00:10;120.0
01.01.2018 00:20;110.0
'''

df = pd.read_csv(StringIO(csv), sep=';')
df = df.set_index(pd.to_datetime(df['Date'], format=r'%d.%m.%Y %H:%M'))  # this line breaks the plotly chart

chart = go.Figure(go.Scatter(x=df.index, y=df['Value']))
ui.plotly(chart)

with ui.pyplot():
    plt.plot(df.index, df['Value'], '-')  # the pyplot still works

ui.run()

Apparently df.index is of type DatetimeIndex, which is not JSON-serializable, even with orjson.
But I found the…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@Nikodemski2002
Comment options

Comment options

You must be logged in to vote
3 replies
@rbeeli
Comment options

@falkoschindler
Comment options

@Nikodemski2002
Comment options

Answer selected by Nikodemski2002
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants