question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Timezone parsing issue with plotly.py JSON serialization

See original GitHub issue

Hello,

This is a code snippet that shows how even when explicitly defining timezones, and stripping out any possible confusion, plot.ly will refuse to display the X axis in the local timezone, instead converting and showing UTC only.

My working code is slightly different, but the incorrect end behavior is the same in this case as well when originating from strings and simple DataFrames

inspecting the fig immediately before being called to draw graph indeed shows the correct US/Eastern timezone, which happens as expected when the x value of the trace is defined.

import plotly.plotly as py
from plotly.graph_objs import *
import pandas as pd
import sys, datetime
from datetime import timedelta

incoming = {
    7887966:    [0.601345, 152.21, 253.12, '2015-04-04 19:31:30'],
    7887967:    [0.075392, 19.08, 253.12, '2015-04-04 19:31:34'],
    7887968:    [0.385279, 97.39, 252.77, '2015-04-04 19:32:13'],
    7887969:    [0.330650, 83.69, 253.12, '2015-04-04 19:32:14'],
    7887970:    [0.110700, 28.02, 253.13, '2015-04-04 19:32:14']}

df = pd.DataFrame.from_dict(incoming, orient='index')
df.columns = ['a','b','y','time']
df['time'] = pd.to_datetime(df['time'])
df = df.set_index('time')

trace = Box(
    x=df.index.tz_localize('UTC').tz_convert('US/Eastern'),
    y=df['y'].values,
    name='timezone issues',
    fillcolor='#a5b404',
    opacity=0.66,
    marker=Marker(
        color='#a5b404'
    )
)

data = Data([trace])

axis_style = dict(
    zeroline=True,
    zerolinecolor='#444',
    zerolinewidth=1,
    gridcolor='#eee',
    autotick=True,
    showline=False
)

layout = Layout(
    title='Timezone offset issue',
    plot_bgcolor='#fff',
    hovermode='closest',
    autosize=True,
    height=610,
    width=1296,
    showlegend=False,
    xaxis=XAxis(
        axis_style,
        autorange=True,
        rangemode='normal',
        tickangle='auto',
        title='Time (note the UTC, when should be US Eastern)',
    ),
    yaxis=YAxis(
        axis_style,
        title='Y axis (no issues here)'
    )
)
fig = Figure(data=data, layout=layout)
py.plot(fig, filename='timezone-issues', world_readable=False)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:23 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
KieranWynncommented, Apr 20, 2017

I would also like to give plotly tz-aware datetimes and have them shown in the provided timezone. Maybe have an API-configurable option to select the viewing timezone?

3reactions
jonmmeasecommented, Mar 26, 2019

Hi all, I haven’t really dug into this yet but it’s definitely important and something that we plan to take care of as a part of version 4. Thanks for helping each other out with workarounds in the meantime!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to overcome "datetime.datetime not JSON serializable"?
My quick & dirty JSON dump that eats dates and everything: json.dumps(my_dictionary, indent=4, sort_keys=True, default=str). default is a function applied ...
Read more >
Plotly uses my local timezone when having dates as data
Hi,. If I understand correctly, Plotly uses the local timezone of the machine by default to avoid DST problem, right?
Read more >
14 Time Zone Handling — Pandas Doc - GitHub Pages
14 Time Zone Handling. Pandas provides rich support for working with timestamps in different time zones using pytz and dateutil libraries. dateutil support ......
Read more >
[Example code]-UTC time zone validation in drf
But I am getting the serializer validation error. "end_date": [ "Datetime has wrong format. Use one of these formats instead: ...
Read more >
Changelog - Streamlit Docs
Introducing a new Streamlit theme for Altair, Plotly, and Vega-Lite charts! ... Bug fixes: Unresponsive pydeck example (#3395), JSON parse error message ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found