-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
df.plot() could cause "ValueError: year XXXXX is out of range" if matplotlib.dates.AutoDateLocator is used #18344
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
Comments
So, IIUC the issue is with matplotlib's AutoDateLocator not understanding DatetimeIndexes, correct? That's not too surprising, since they don't depend on pandas. We provide our own locators / formatters for our dtypes, which is why the default |
Yes, it is probably. But I don’t have clear idea of what’s root cause basically; Moreover, I’m not familiar with Pandas nor matplotlib. Do you mean it’s not issue in Pandas but in my usage of non-Pandas locator with Pandas? Or is it rather problem in matplotlib? Thanks. |
As I found https://github.com/pandas-dev/pandas/blob/c3cfe90d8b7be9435c59f279fc933c7931f0e215/pandas/plotting/_converter.py, it is probably caused by my fault. I’ll close this issue after playing locators there. Thanks. |
Both :) I suspect matplotlib's AutoDateLocator doesn't know how to deal with pandas' datetimes. Though there has been some work there recently, so maybe try with matplotlib master: https://github.com/matplotlib/matplotlib/pull/9794/files |
Neither helps, PandasAutoDateLocator nor matplotlib from master branch... from datetime import datetime
from matplotlib import pyplot as plt
import pandas as pd
from pandas.tseries.converter import PandasAutoDateLocator
N = 2
index = pd.date_range(
datetime.now().replace(second=0, microsecond=0),
periods=N,
freq='60Min',
)
df = pd.DataFrame({"A": range(N)}, index)
ax = df.plot()
ax.xaxis.set_major_locator(PandasAutoDateLocator())
print(df)
plt.show()
|
Is there a problem with the default formatter picked by pandas? You're welcome to dig into |
@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. |
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. |
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
Stacktrace
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...
The text was updated successfully, but these errors were encountered: