3

Trying to plot data using the matplotlib.pyplot.plot_date function with datetime objects originating from the netCDF4.num2date function I get the following error:

In [1]: from netCDF4 import num2date

In [2]: from matplotlib.pyplot import plot_date

In [3]: d=num2date((86400,2*86400),"seconds since 2000-01-01")

In [4]: gca().plot_date(d,(0,1))
...
AttributeError: 'cftime._cftime.DatetimeGregorian' object has no attribute 'toordinal'

The above exception was the direct cause of the following exception:
ConversionError
...
ConversionError: Failed to convert value(s) to axis units: array([cftime.DatetimeGregorian(2000-01-02 00:00:00),
       cftime.DatetimeGregorian(2000-01-03 00:00:00)], dtype=object)

The following package versions are installed:

pandas 1.0.3

matplotlib 3.2.1

netcdf4 1.5.1.2

cftime 1.1.1.2

As the same thing perfectly works on a different machine with older package versions, I assume a version issue. Also, I've tried the solution suggested in this thread and other related threads which seemed like a similar issue, but

from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

didn't help neither.

Any suggestions welcome;)

momme
  • 141
  • 9

1 Answers1

3

Found the answer myself, from version 1.1.0 the num2date function in the cftime package changed its default behaviour to return cftime datetime instances instead of python datetime instances where possible (the option argument only_use_cftime_datetimes is now True by default, instead of False). The plot_date function however, at least currently, doesn't handle these. To avoid the issue, use the arguments only_use_cftime_dateimes=False and only_use_python_datetime=True or use the convenience function num2pydate.

momme
  • 141
  • 9