diff --git a/doc/whats-new.rst b/doc/whats-new.rst index e8ce0cfffba..aa58a982973 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,6 +37,8 @@ New Features - Allow creating :py:class:`xr.Coordinates` objects with no indexes (:pull:`8711`) By `Benoit Bovy `_ and `Tom Nicholas `_. +- Enable plotting of ``datetime.dates``. (:issue:`8866`, :pull:`8873`) + By `Sascha Hofmann `_. Breaking changes ~~~~~~~~~~~~~~~~ diff --git a/xarray/plot/utils.py b/xarray/plot/utils.py index 804e1cfd795..8789bc2f9c2 100644 --- a/xarray/plot/utils.py +++ b/xarray/plot/utils.py @@ -4,7 +4,7 @@ import textwrap import warnings from collections.abc import Hashable, Iterable, Mapping, MutableMapping, Sequence -from datetime import datetime +from datetime import date, datetime from inspect import getfullargspec from typing import TYPE_CHECKING, Any, Callable, Literal, overload @@ -672,7 +672,7 @@ def _ensure_plottable(*args) -> None: np.bool_, np.str_, ) - other_types: tuple[type[object], ...] = (datetime,) + other_types: tuple[type[object], ...] = (datetime, date) cftime_datetime_types: tuple[type[object], ...] = ( () if cftime is None else (cftime.datetime,) ) diff --git a/xarray/tests/test_plot.py b/xarray/tests/test_plot.py index 6f983a121fe..8da1bba6207 100644 --- a/xarray/tests/test_plot.py +++ b/xarray/tests/test_plot.py @@ -5,7 +5,7 @@ import math from collections.abc import Hashable from copy import copy -from datetime import datetime +from datetime import date, datetime, timedelta from typing import Any, Callable, Literal import numpy as np @@ -620,6 +620,18 @@ def test_datetime_dimension(self) -> None: ax = plt.gca() assert ax.has_data() + def test_date_dimension(self) -> None: + nrow = 3 + ncol = 4 + start = date(2000, 1, 1) + time = [start + timedelta(days=i) for i in range(nrow)] + a = DataArray( + easy_array((nrow, ncol)), coords=[("time", time), ("y", range(ncol))] + ) + a.plot() + ax = plt.gca() + assert ax.has_data() + @pytest.mark.slow @pytest.mark.filterwarnings("ignore:tight_layout cannot") def test_convenient_facetgrid(self) -> None: