Pandas: BUG: register_matplotlib_converters leads to wrong datetime interpretation with matplotlib 3.3

Created on 20 Jul 2020  路  7Comments  路  Source: pandas-dev/pandas

  • [x] I have checked that this issue has not already been reported.

  • [x] I have confirmed this bug exists on the latest version of pandas.

  • [ ] (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

import datetime
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

t = [
    datetime.datetime(2022, 12, 31, 0, 0), 
    datetime.datetime(2022, 11, 30, 0, 0), 
    datetime.datetime(2022, 10, 31, 0, 0),
]
s = [0.0, 0.1, 0.2]

fig, ax = plt.subplots()
ax.plot_date(x=t, y=s)
ax.xaxis.set_major_formatter(DateFormatter("%b '%y"))

plt.xticks(rotation=90)
plt.show()

Problem description

The years in the plot are now 91 and 92.

grafik

Note: With matplotlib 3.2.2 (the last stable release) the error is not there. So I guess this is a bug due to an api change inside matplotlib 3.3.0.

Expected Output

I expects the year 22 and 23 like in this image:

grafik

The second image can be produced by deleting the call to register_matplotlib_converters()

Output of pd.show_versions()

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

pandas           : 1.0.5
numpy            : 1.19.0
pytz             : 2020.1
dateutil         : 2.8.1
pip              : 19.2.3
setuptools       : 41.2.0
Cython           : None
pytest           : None
hypothesis       : None
sphinx           : None
blosc            : None
feather          : None
xlsxwriter       : None
lxml.etree       : None
html5lib         : None
pymysql          : None
psycopg2         : None
jinja2           : None
IPython          : None
pandas_datareader: None
bs4              : None
bottleneck       : None
fastparquet      : None
gcsfs            : None
lxml.etree       : None
matplotlib       : 3.3.0
numexpr          : None
odfpy            : None
openpyxl         : None
pandas_gbq       : None
pyarrow          : None
pytables         : None
pytest           : None
pyxlsb           : None
s3fs             : None
scipy            : None
sqlalchemy       : None
tables           : None
tabulate         : None
xarray           : None
xlrd             : None
xlwt             : None
xlsxwriter       : None
numba            : None
Bug Visualization

Most helpful comment

I can confirm, that using plt.rcParams['date.epoch'] = '0000-12-31T00:00:00' as a workaround fixes the issue.

import datetime
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

plt.rcParams['date.epoch'] = '0000-12-31T00:00:00'

t = [
    datetime.datetime(2022, 12, 31, 0, 0), 
    datetime.datetime(2022, 11, 30, 0, 0), 
    datetime.datetime(2022, 10, 31, 0, 0),
]
s = [0.0, 0.1, 0.2]

fig, ax = plt.subplots()
ax.plot_date(x=t, y=s)
ax.xaxis.set_major_formatter(DateFormatter("%b '%y"))

plt.xticks(rotation=90)
plt.show()

All 7 comments

@ZahlGraf do you see anything in the release notes that indicates what might have changed?

Unfortunately not really. There are some changes to ticks, but they are not really related to dates:
https://matplotlib.org/3.3.0/users/whats_new.html

I also remember a deprecation warning about epoch2num, coming from pandas code. But there is already an issue for that (https://github.com/pandas-dev/pandas/issues/34850) here and furthermore it is just a warning about upcoming changes.

cc @jklymak on the hunch that this is related to the variable epoch changes in https://github.com/matplotlib/matplotlib/pull/15008, but that's just a guess.

I'll hopefully have time to look more closely later in the week.

Yes our epoch changed from 0000-12-31T00:00:00 to 1970-01-01T00:00:00. You can check for the old behaviour via
plt.rcParams['date.epoch'] = '0000-12-31T00:00:00' and see if that fixes things.

I don't know why pandas relies on matplotlib's epoch - presumably you use mdates.date2num and its now incorrect. However, if at all possible its helpful to use a more modern epoch to give more resolution in floating point times.

I can confirm, that using plt.rcParams['date.epoch'] = '0000-12-31T00:00:00' as a workaround fixes the issue.

import datetime
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

plt.rcParams['date.epoch'] = '0000-12-31T00:00:00'

t = [
    datetime.datetime(2022, 12, 31, 0, 0), 
    datetime.datetime(2022, 11, 30, 0, 0), 
    datetime.datetime(2022, 10, 31, 0, 0),
]
s = [0.0, 0.1, 0.2]

fig, ax = plt.subplots()
ax.plot_date(x=t, y=s)
ax.xaxis.set_major_formatter(DateFormatter("%b '%y"))

plt.xticks(rotation=90)
plt.show()

Glad there is a workaround, and sorry for the inconvenience. I should have pinged pandas when the change went in. https://matplotlib.org/3.3.0/users/whats_new.html#dates-use-a-modern-epoch

Was this page helpful?
0 / 5 - 0 ratings

Related issues

amelio-vazquez-reina picture amelio-vazquez-reina  路  3Comments

Abrosimov-a-a picture Abrosimov-a-a  路  3Comments

matthiasroder picture matthiasroder  路  3Comments

hiiwave picture hiiwave  路  3Comments

ericdf picture ericdf  路  3Comments