Skip to content

Regression in datetime handling in plots #6102

@jklymak

Description

@jklymak

#5794 (ea28861) introduced a regression in whether or not pandas datetime converters are loaded or Matplotlib's. This leads to basic Matplotlib-native plotting failing matplotlib/matplotlib#22023 Previously matplotlib's converters were loaded, now pandas are being loaded, despite the downstream user not ever using xarray's plotting utilities.

test code

import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import matplotlib.units as munits
print(munits.registry)
ds = xr.Dataset({"time": [np.datetime64('2000-01-01'), np.datetime64('2000-01-02')],
                     "sir": [0, 1]})

fig, ax = plt.subplots()
# crashes:
ax.scatter(ds['time'], ds['sir'])
plt.show()

Previously:

{... 
<class 'numpy.datetime64'>: <matplotlib.dates._SwitchableDateConverter object at 0x106434ac0>, 
<class 'datetime.date'>: <matplotlib.dates._SwitchableDateConverter object at 0x106434ac0>, 
<class 'datetime.datetime'>: <matplotlib.dates._SwitchableDateConverter object at 0x106434ac0>}

Now:

{... <class 'numpy.datetime64'>: <pandas.plotting._matplotlib.converter.DatetimeConverter object at 0x17f288160>, 
<class 'datetime.date'>: <pandas.plotting._matplotlib.converter.DatetimeConverter object at 0x17f182250>, 
<class 'datetime.datetime'>: <pandas.plotting._matplotlib.converter.DatetimeConverter object at 0x17f1821c0>, 
<class 'pandas._libs.tslibs.timestamps.Timestamp'>: <pandas.plotting._matplotlib.converter.DatetimeConverter object at 0x17f16ff40>, 
<class 'pandas._libs.tslibs.period.Period'>: <pandas.plotting._matplotlib.converter.PeriodConverter object at 0x17f16ffa0>, 
<class 'datetime.time'>: <pandas.plotting._matplotlib.converter.TimeConverter object at 0x17f288130>}

As you can see, the pandas converters have been loaded without any use of pandas nor xarray plotting utilities.

Suggestion

Of course if xarray plotting is loaded, you should use and register what date converters you would like (I'd suggest matplotlib.dates.ConciseConverter, but your mileage may vary). But I think if the user is just trying to use xarray to load a data set, they should not have decisions made for them about the converter (or any other plotting functions), and to prevent confusion they should get the default matplotlib converter since it handles datetime64 just fine.

I think it could also be argued that this is a pandas issue, in that just importing pandas should not automatically register their converters unless their plotting is used. ping @TomAugspurger because I thought that was the plan, but apparently things changed. And it indeed appears their converter has a bug in it for matplotlib scatter.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions