Skip to content

Commit 852b7e6

Browse files
Add dt.date to plottable types (#8873)
* Add dt.date to plottable types * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add new feature to whats-new.rst * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add test_date_dimension to plot tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add issue to whats-new * Fix type in whats-new * Fix 2 more typos in whats-new --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 5bf2cf4 commit 852b7e6

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ New Features
3737
- Allow creating :py:class:`xr.Coordinates` objects with no indexes (:pull:`8711`)
3838
By `Benoit Bovy <https://github.com/benbovy>`_ and `Tom Nicholas
3939
<https://github.com/TomNicholas>`_.
40+
- Enable plotting of ``datetime.dates``. (:issue:`8866`, :pull:`8873`)
41+
By `Sascha Hofmann <https://github.com/saschahofmann>`_.
4042

4143
Breaking changes
4244
~~~~~~~~~~~~~~~~

xarray/plot/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import textwrap
55
import warnings
66
from collections.abc import Hashable, Iterable, Mapping, MutableMapping, Sequence
7-
from datetime import datetime
7+
from datetime import date, datetime
88
from inspect import getfullargspec
99
from typing import TYPE_CHECKING, Any, Callable, Literal, overload
1010

@@ -672,7 +672,7 @@ def _ensure_plottable(*args) -> None:
672672
np.bool_,
673673
np.str_,
674674
)
675-
other_types: tuple[type[object], ...] = (datetime,)
675+
other_types: tuple[type[object], ...] = (datetime, date)
676676
cftime_datetime_types: tuple[type[object], ...] = (
677677
() if cftime is None else (cftime.datetime,)
678678
)

xarray/tests/test_plot.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import math
66
from collections.abc import Hashable
77
from copy import copy
8-
from datetime import datetime
8+
from datetime import date, datetime, timedelta
99
from typing import Any, Callable, Literal
1010

1111
import numpy as np
@@ -620,6 +620,18 @@ def test_datetime_dimension(self) -> None:
620620
ax = plt.gca()
621621
assert ax.has_data()
622622

623+
def test_date_dimension(self) -> None:
624+
nrow = 3
625+
ncol = 4
626+
start = date(2000, 1, 1)
627+
time = [start + timedelta(days=i) for i in range(nrow)]
628+
a = DataArray(
629+
easy_array((nrow, ncol)), coords=[("time", time), ("y", range(ncol))]
630+
)
631+
a.plot()
632+
ax = plt.gca()
633+
assert ax.has_data()
634+
623635
@pytest.mark.slow
624636
@pytest.mark.filterwarnings("ignore:tight_layout cannot")
625637
def test_convenient_facetgrid(self) -> None:

0 commit comments

Comments
 (0)