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

Problem plotting historical time series #1402

Closed
elysiumfield opened this issue Jan 23, 2019 · 7 comments
Closed

Problem plotting historical time series #1402

elysiumfield opened this issue Jan 23, 2019 · 7 comments
Labels
bug something broken
Milestone

Comments

@elysiumfield
Copy link

elysiumfield commented Jan 23, 2019

I have recently updated plotly to ver 3.5.0 and am having problems plotting historical time series. The test code is as follows:

import numpy as np
import pandas as pd
import plotly.graph_objs as go
import plotly.offline as pyo
dt = pd.date_range(start='1969-12-31', periods=270, freq='Q')
t1 = []
t1.append(dict(x=dt, y=np.ones(270), name='cape', type='scatter'))
layout = go.Layout(title="test", width=1800, height=930, font=dict(size=27), titlefont=dict(size=42))
fig = go.Figure(data=t1, layout=layout)
pyo.plot(fig, filename='test.html', auto_open=False, show_link=False)

This code returns the error:

  File "C:\ProgramData\Anaconda3\lib\site-packages\plotly\offline\offline.py", line 645, in plot
    '100%', '100%', global_requirejs=False)

  File "C:\ProgramData\Anaconda3\lib\site-packages\plotly\offline\offline.py", line 336, in _plot_html
    jdata = _json.dumps(figure.get('data', []), cls=utils.PlotlyJSONEncoder)

  File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)

  File "C:\ProgramData\Anaconda3\lib\site-packages\plotly\utils.py", line 168, in encode
    encoded_o = super(PlotlyJSONEncoder, self).encode(o)

  File "C:\ProgramData\Anaconda3\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)

  File "C:\ProgramData\Anaconda3\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)

  File "C:\ProgramData\Anaconda3\lib\site-packages\plotly\utils.py", line 233, in default
    return encoding_method(obj)

  File "C:\ProgramData\Anaconda3\lib\site-packages\plotly\utils.py", line 297, in encode_as_datetime
    obj = obj.astimezone(pytz.utc)

OSError: [Errno 22] Invalid argument

However, defining dt = pd.date_range(start='1970-03-31', periods=270, freq='Q'), everything works fine. I am positive that previous versions of plotly could plot datetimeindex that was older.

@jonmmease
Copy link
Contributor

Hi @elysiumfield, thanks for the report.

Could you add the versions of Python and pandas that you're using?

I'm not able to reproduce the error with Python 3.6, pandas 0.23, and and plotly 3.5.0 on MacOS. I get the following figure:
newplot 19

Thanks!

@jonmmease
Copy link
Contributor

Also, have you seen this Stack Overflow thread? https://stackoverflow.com/questions/37494983/python-fromtimestamp-oserror

Could you try the code example from this comment https://stackoverflow.com/a/45372194/4551895:

import datetime
import platform
print(
    "Running on Python ver.{} on {} {}\n" \
    .format(
        platform.python_version(),
        platform.system(),
        platform.release()
        )
)
for timestamp in range(1, 100000000):
    try:
        dt = datetime.datetime.fromtimestamp(timestamp)
    except:
        pass
    else:
        break
print(
    "Smallest accepted Unix timestamp by {}: '{}' ({})" \
    .format(platform.system(), timestamp, dt)
)

On OS X I get

Running on Python ver.3.6.8 on Darwin 18.0.0

Smallest accepted Unix timestamp by Darwin: '1' (1969-12-31 19:00:01)

@elysiumfield
Copy link
Author

Hi @jonmmease, I think you are on the right track. I am using Python 3.7 and Pandas 0.23.4. Running the code you posted, I got:

Running on Python ver.3.7.1 on Windows 7
Smallest accepted Unix timestamp by Windows: '1' (1970-01-01 01:00:01)

I also get the same problem as in the link you posted, but it doesn't say how to solve it.

@elysiumfield
Copy link
Author

I have tested a bit more. Tried my test code on another other computer and it worked. That one is having:

Running on Python ver.3.6.4 on Windows 7
Smallest accepted Unix timestamp by Windows: '86400' (1970-01-02 01:00:00)

I updated Plotly to ver 3.5.0 and it still works. But datetime.datetime.fromtimestamp(-43201, pytz.utc) gives [Errno 22] error.

Not sure that my plot problem is related to datetime in Windows after all. The main difference I know of between my test on the two different computers is python 3.7 vs 3.6, but there are probably more differences due to dependencies on other packages.

@mprostock
Copy link
Contributor

Problem can be changed by a 1-character-fix in the code ;-)
See PR : #1501

@elysiumfield
Copy link
Author

OK great, thanks.

@jonmmease jonmmease added bug something broken and removed question labels Apr 12, 2019
@jonmmease jonmmease added this to the v3.8.0 milestone Apr 12, 2019
@jonmmease
Copy link
Contributor

Fixed by #1501

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug something broken
Projects
None yet
Development

No branches or pull requests

3 participants