Skip to content
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

BUG: epoch2num matplotlib deprecation warning #34850

Closed
3 tasks done
dstansby opened this issue Jun 17, 2020 · 14 comments · Fixed by #35393
Closed
3 tasks done

BUG: epoch2num matplotlib deprecation warning #34850

dstansby opened this issue Jun 17, 2020 · 14 comments · Fixed by #35393
Labels
Blocker Blocking issue or pull request for an upcoming release Deprecate Functionality to remove in pandas Visualization plotting
Milestone

Comments

@dstansby
Copy link
Contributor

dstansby commented Jun 17, 2020

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

  • 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.


Problem description

Using the current matplotlib master branch, pandas raises the following deprecation warning:

/Users/dstansby/miniconda3/envs/dev/lib/python3.8/site-packages/pandas/plotting/_matplotlib/converter.py:256: MatplotlibDeprecationWarning: 
The epoch2num function was deprecated in Matplotlib 3.3 and will be removed two minor releases later.

It looks like this line is present on pandas master, so should probably be changed (I'm not sure how) to avoid raising this deprecation warning.

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.3.final.0
python-bits : 64
OS : Darwin
OS-release : 19.4.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.0.4
numpy : 1.18.5
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 47.1.1.post20200604
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : 3.0.4
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.1
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.15.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.1
matplotlib : 3.2.1.post2858+gc3bfeb9c3
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.3
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@dstansby dstansby added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 17, 2020
@charlesdong1991 charlesdong1991 added Deprecate Functionality to remove in pandas Visualization plotting and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 17, 2020
@igavronski
Copy link

Same here:

pip list
Package         Version
--------------- ---------
certifi         2020.6.20
chardet         3.0.4
cycler          0.10.0
idna            2.10
kiwisolver      1.2.0
matplotlib      3.3.0
numpy           1.19.0
opencv-python   4.3.0.36
pandas          1.0.5
Pillow          7.2.0
pyparsing       2.4.7
python-dateutil 2.8.1
pytz            2020.1
requests        2.24.0
six             1.15.0
urllib3         1.25.9

python -V
Python 3.8.4

Platform: Windows 10

@igavronski
Copy link

@misantroop
Copy link

Is there a workaround or how to approach this issue?

@0x26res
Copy link

0x26res commented Jul 20, 2020

Is there a workaround or how to approach this issue?

If you really need a workaround you can monkey patch pandas.plotting._converter._dt_to_float_ordinal which calls the deprecated function dates.epoch2num

Maybe there's a cleaner way, but I can't think of one.

import matplotlib.dates as dates
import numpy as np
import pandas as pd
import warnings

from pandas.core.dtypes.common import is_datetime64_ns_dtype
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.index import Index


def my_dt_to_float_ordinal(dt):
    """
    Convert :mod:`datetime` to the Gregorian date as UTC float days,
    preserving hours, minutes, seconds and microseconds.  Return value
    is a :func:`float`.
    """
    if (isinstance(dt, (np.ndarray, Index, ABCSeries)
                   ) and is_datetime64_ns_dtype(dt)):
        base = dates.EPOCH_OFFSET + np.asarray(dt.asi8 / 1.0E9) / dates.SEC_PER_DAY
    else:
        base = dates.date2num(dt)
    return base


import pandas.plotting._converter

pandas.plotting._converter._dt_to_float_ordinal = my_dt_to_float_ordinal

df = pd.DataFrame(
    {
        "datetime": [pd.Timestamp("2020-01-01", tz="UTC"), pd.Timestamp("2020-01-02", tz="UTC")],
        "value_1": [1.0, 2.0],
        "value_2": [3.0, 4.0],
    }
).set_index('datetime')

with warnings.catch_warnings(record=True) as w:
    df.plot()
assert 0 == len(w)

@igavronski
Copy link

I have patched the local file converter.py using @0x26res suggestion.
In fact, the error message went away, but it calculates the year as 3989...
I replaced line 256 for:

base = dates.EPOCH_OFFSET + np.asarray(dt.asi8 / 1.0E9) / dates.SEC_PER_DAY

@TomAugspurger TomAugspurger added this to the 1.1 milestone Jul 20, 2020
@TomAugspurger TomAugspurger added the Blocker Blocking issue or pull request for an upcoming release label Jul 20, 2020
@igavronski
Copy link

I confirm that modifying the file converter.py (in my Windows 10 installation is located in %USERPROFILE%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\plotting\_matplotlib),
replacing line 256, by:

base = dates.EPOCH_OFFSET + np.asarray(dt.asi8 / 1.0E9) / dates.SEC_PER_DAY

and inserting the following code in lines 33-34:

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

circumvents the problem while developers don't provide a newer, patched code for pd.plot()
Please refer to #35350 (comment) and #34850 (comment)

@jklymak
Copy link
Contributor

jklymak commented Jul 20, 2020

An alternative is matplotlib patches epoch2num to use the new epoch machinery. We didn't use this function internally and didn't test it so derecation seemed reasonable. However if pandas uses it we can fix from our end for back compatibility.

Longer term it might be good to homogenize pandas and matplotlib's date handling. It causes confusion for them to be different, and I suspect that python date handling has matured significantly since either pandas or matplotlib started implementing support.

@TomAugspurger
Copy link
Contributor

I'll try to look into this over the next day or two. Agreed that we should be relying on matplotlib's date handling as much as possible.

@Haaroon
Copy link

Haaroon commented Jul 20, 2020

I confirm that modifying the file converter.py (in my Windows 10 installation is located in %USERPROFILE%\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\pandas\plotting\_matplotlib),
replacing line 256, by:

base = dates.EPOCH_OFFSET + np.asarray(dt.asi8 / 1.0E9) / dates.SEC_PER_DAY

and inserting the following code in lines 33-34:

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

circumvents the problem while developers don't provide a newer, patched code for pd.plot()
Please refer to #35350 (comment) and #34850 (comment)

@igavronski Temp workaround fixed the issue for the time being.

@jklymak
Copy link
Contributor

jklymak commented Jul 20, 2020

I think I fixed this on the Matplotlib side with matplotlib/matplotlib#17983. I also removed the deprecation since its not fair to ask your users to filter the warning. OTOH, I think that in general, we would prefer to not maintain epoch2num, and num2epoch.

@satishm75
Copy link

In my case, I found converter.py at the following location:

C:\Users\USERNAME\AppData\Local\Programs\Python\Python37\Lib\site-packages\pandas\plotting_matplotlib
Added import statement in the import section and plt.rcParams['date.epoch'] = '0000-12-31T00:00:00' in the constant section.

@chrisconlan
Copy link

As of July 21st, installing the latest pandas and matplotlib allows plots to work, but the dates are screwed up on the x-axis for datetime-indexed data frames. Installing Matplotlib 3.2.2 as opposed to 3.3.0 fixes the x-axis formatting for now.

@TomAugspurger
Copy link
Contributor

Could people post reproducible examples of plots that look different between mpl 3.2.2 and 3.3.0 with pandas 1.0.5?

@chrisconlan
Copy link

chrisconlan commented Jul 23, 2020

import pandas as pd
import matplotlib.pyplot as plt
import datetime
from datetime import timedelta

series = pd.Series(list(range(10000)), index=[datetime.datetime.now() + timedelta(hours=i, minutes=i) for i in range(10000)])
series.plot()
plt.show()

This figure ...

  • Python 3.7
  • Pandas 1.0.5
  • Matplotlib 3.3.0
  • Raises lib/python3.7/site-packages/pandas/plotting/_matplotlib/converter.py:256: MatplotlibDeprecationWarning: The epoch2num function was deprecated in Matplotlib 3.3 and will be removed two minor releases later. base = dates.epoch2num(dt.asi8 / 1.0e9)

mpl_330_pd_105

This figure ...

  • Python 3.7
  • Pandas 1.0.5
  • Matplotlib 3.2.2

mpl_322_pd_105

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Blocker Blocking issue or pull request for an upcoming release Deprecate Functionality to remove in pandas Visualization plotting
Projects
None yet
10 participants