df.plot() could cause "ValueError: year XXXXX is out of range" if matplotlib.dates.AutoDateLocator is used

See original GitHub issue

I encountered really wired problem around at combination of df.plot(), matplotlib.dates.AutoDateLocator and so on. For more details, please see below code that can reproduce the error everywhere as long as I checked.

Code to reproduce the error

from datetime import datetime
from matplotlib import pyplot as plt
from matplotlib.dates import AutoDateLocator
import pandas as pd

N = 2
index = pd.date_range(
    datetime.now().replace(second=0, microsecond=0),  # If second!=0, then the error disappears.
    periods=N,
    freq='60Min',  # If freq='H', then the error disappears.
)

df = pd.DataFrame({"A": range(N)}, index)
ax = df.plot()

# Unless AutoDateLocator is used, then the error disappears.
#   i.e.
#   - AutoDateLocator -> error
#   - MicrosecondLocator -> no error
#   - YearLocator -> no error
# Besides, it doesn't matter to which you set the locator, major or minor.
ax.xaxis.set_major_locator(AutoDateLocator())

print(df)
plt.show()

Stacktrace

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Python36\lib\tkinter\__init__.py", line 745, in callit
    func(*args)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 323, in idle_draw
    self.draw()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 304, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_agg.py", line 430, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axes\_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 1133, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 974, in _update_ticks
    tick_tups = list(self.iter_ticks())
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 917, in iter_ticks
    majorLocs = self.major.locator()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 1054, in __call__
    self.refresh()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 1074, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 832, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 441, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 256, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: year 68948 is out of range
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 233, in resize
    self.show()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 304, in draw
    FigureCanvasAgg.draw(self)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\backends\backend_agg.py", line 430, in draw
    self.figure.draw(self.renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\figure.py", line 1295, in draw
    renderer, self, artists, self.suppressComposite)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axes\_base.py", line 2399, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\image.py", line 138, in _draw_list_compositing_images
    a.draw(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 1133, in draw
    ticks_to_draw = self._update_ticks(renderer)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 974, in _update_ticks
    tick_tups = list(self.iter_ticks())
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\axis.py", line 917, in iter_ticks
    majorLocs = self.major.locator()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 1054, in __call__
    self.refresh()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 1074, in refresh
    dmin, dmax = self.viewlim_to_dt()
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 832, in viewlim_to_dt
    return num2date(vmin, self.tz), num2date(vmax, self.tz)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 441, in num2date
    return _from_ordinalf(x, tz)
  File "C:\Users\sakurai\Desktop\ENV\lib\site-packages\matplotlib\dates.py", line 256, in _from_ordinalf
    dt = datetime.datetime.fromordinal(ix).replace(tzinfo=UTC)
ValueError: year 68948 is out of range

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.6.1.final.0 python-bits: 64 OS: Windows OS-release: 10 machine: AMD64 processor: Intel64 Family 6 Model 42 Stepping 7, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None

pandas: 0.21.0 pytest: None pip: 9.0.1 setuptools: 28.8.0 Cython: None numpy: 1.13.3 scipy: None pyarrow: None xarray: None IPython: None sphinx: None patsy: None dateutil: 2.6.1 pytz: 2017.3 blosc: None bottleneck: None tables: None numexpr: None feather: None matplotlib: 2.1.0 openpyxl: None xlrd: None xlwt: None xlsxwriter: None lxml: None bs4: None html5lib: None sqlalchemy: None pymysql: None psycopg2: None jinja2: None s3fs: None fastparquet: None pandas_gbq: None pandas_datareader: None

Windows + Python 3.6 + the newest libraries

(ENV) C:\Users\sakurai\Desktop>python -VV Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]

(ENV) C:\Users\sakurai\Desktop>python -m pip freeze cycler==0.10.0 matplotlib==2.1.0 numpy==1.13.3 pandas==0.21.0 pyparsing==2.2.0 python-dateutil==2.6.1 pytz==2017.3 six==1.11.0

Windows + Python 3.5 + old libraries

C:\Users\sakurai\Desktop>C:\Python35\python.exe -c "import sys; print(sys.version)" 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)]

C:\Users\sakurai\Desktop>C:\Python35\python.exe -m pip freeze | findstr “pandas matplotlib” matplotlib==1.5.3 pandas==0.19.1

Debian + Python 3.6 + the newest libraries

root@0bf58ce62933:/# python -VV Python 3.6.3 (default, Nov 4 2017, 22:17:09) [GCC 4.9.2] root@0bf58ce62933:/# python -m pip freeze cycler==0.10.0 matplotlib==2.1.0 numpy==1.13.3 pandas==0.21.0 pyparsing==2.2.0 python-dateutil==2.6.1 pytz==2017.3 six==1.11.0

Problem description

Empty window is popped up like this if the error occurs. image

Expected result

Graph should be drawn there at least…

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
joshmalinacommented, Feb 5, 2019

So this is not a fix, but if you call .to_timestamp() on the dataframe before you call plot, it converts it from a PeriodIndex to a DatetimeIndex, and then the issue is no longer there. It would be nice if this was fixed for PeriodIndex.

0reactions
sakurai-youheicommented, Nov 20, 2017

@TomAugspurger Thanks for your guidance. Actually, problem was there’s no relevant information in the Internet and problem for me right now is default locators look slightly different from AutoXxxLocator. 😃 If I’ll have found root, I will suggest something here or through pull-request. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error with year when using DateFormatter with pandas
I'm having a similar problem to this, but that post didn't offer a minimal example. Does anyone have advice?
Read more >
Matplotlib.pdf
If you are on Linux, you might prefer to use your package manager. ... Plotting requires action on a range of levels, from...
Read more >
Matplotlib - Computer Science
matplotlib is a library for making 2D plots of arrays in Python. Although it has its origins in emulating the. MATLAB® 1 graphics...
Read more >
matplotlib.pyplot.subplots Example - Program Talk
Axes The axes to plot to, if none, make new figure + axes probe_size : float, optional If not None, use as radius...
Read more >
929d7baa32c9e105f84c5f2c29e...
Option 1 s = s.sort_index() df.plot(color = s.values) # as per ... import json # or `import simplejson as json` if on Python...
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