diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index bd27bef3759a..ac79c441ab9a 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -207,9 +207,9 @@ def _get_rc_timezone(): def _to_ordinalf(dt): """ - Convert :mod:`datetime` or :mod:`date` to the Gregorian date as UTC float - days, preserving hours, minutes, seconds and microseconds. Return value - is a :func:`float`. + Convert :mod:`datetime`, :mod:`date` or :mod:`timedelta` to the Gregorian + date as UTC float days, preserving hours, minutes, seconds and + microseconds. Return value is a :func:`float`. """ # Convert to UTC tzi = getattr(dt, 'tzinfo', None) @@ -217,6 +217,9 @@ def _to_ordinalf(dt): dt = dt.astimezone(UTC) tzi = UTC + if isinstance(dt, datetime.timedelta): + return dt.total_seconds() / SEC_PER_DAY + base = float(dt.toordinal()) # If it's sufficiently datetime-like, it will have a `date()` method diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index 5a25e6182b7e..003789951598 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -466,3 +466,10 @@ def test_tz_utc(): def test_num2timedelta(x, tdelta): dt = mdates.num2timedelta(x) assert dt == tdelta + + +def test_timedelta_ordinalf(): + # Check that timedeltas can be converted to ordinalfs + dt = datetime.timedelta(seconds=60) + ordinalf = mdates._to_ordinalf(dt) + assert ordinalf == 1 / (24 * 60)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: