df.plot() could cause "ValueError: year XXXXX is out of range" if matplotlib.dates.AutoDateLocator is used
See original GitHub issueI 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()
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 -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 -m pip freeze | findstr “pandas matplotlib” matplotlib==1.5.3 pandas==0.19.1
Debian + Python 3.6 + the newest libraries
Problem description
Empty window is popped up like this if the error occurs.
Expected result
Graph should be drawn there at least…
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
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.
@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.