Failed to format x-axis as Timestamp
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
plt.plot(ts.index,ts)
plt.show()
x-axis is show as raw number of epoch time in nanoseconds (~1.5e18). However, if we call ts.plot()
once before calling plt.plot
, x-axis is correctly formatted and the correct behavior will persist for the entire python session, even if we're creating new Series or DataFrame.
I recently upgraded from pandas 0.17.1 to 0.21.0 and the older version did not have this issue.
Installed Versions
pd.show_versions()
INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 2.6.32-642.11.1.el6.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8
pandas: 0.21.0
pytest: None
pip: 9.0.1
setuptools: 20.1.1
Cython: None
numpy: 1.13.3
scipy: 0.17.0
pyarrow: None
xarray: None
IPython: 4.1.1
sphinx: None
patsy: None
dateutil: 2.6.1
pytz: 2017.3
blosc: None
bottleneck: 1.0.0
tables: None
numexpr: 2.6.4
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
Comment From: yakouyang
It seems that the older version‘s bug reappears. https://stackoverflow.com/questions/26526230/plotting-datetimeindex-on-x-axis-with-matplotlib-creates-wrong-ticks-in-pandas-0/26529774#26529774 I solved this problem by the method in link. " you can convert the index to python datetime's using to_pydatetime() "
Comment From: jorisvandenbossche
See http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#no-automatic-matplotlib-converters.
We will change this back temporarily to issue a informative deprecation warning in an upcoming 0.21.1 release.
Duplicate of https://github.com/pandas-dev/pandas/issues/18301
Comment From: weakunix
Thanks vm for the quick response - indeed a deprecation warning helps when someone (like myself) fails to read the what's new page when updating pandas :)
After reading issue #18301, it appears that this is done to improve import time as these converters may not be needed and matplotlib could be implementing support for pandas datetime64 anyway. However until matplotlib does, I'm putting those two lines of converter registration just below import pandas
which basically restores the auto registration on import. I wonder if it's really worth the tiny saving of import time to disable auto registration before matplotlib is ready (python never import the same module twice anyway).
Comment From: jorisvandenbossche
Yes, I was actually wondering the same yesterday: https://github.com/pandas-dev/pandas/pull/18307#issuecomment-346190497