From b8c2abf75452f88e841d5968e1437dc07a599c5c Mon Sep 17 00:00:00 2001 From: David Stansby Date: Wed, 12 Jul 2017 10:04:18 +0100 Subject: [PATCH 1/2] Add num2timedelta method with test --- lib/matplotlib/dates.py | 32 ++++++++++++++++++++++++++++++ lib/matplotlib/tests/test_dates.py | 9 +++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index 0d54c69f90e0..97216921d9b5 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -406,6 +406,38 @@ def num2date(x, tz=None): return _from_ordinalf_np_vectorized(x, tz).tolist() +def _ordinalf_to_timedelta(x): + return datetime.timedelta(days=x) + + +_ordinalf_to_timedelta_np_vectorized = np.vectorize(_ordinalf_to_timedelta) + + +def num2timedelta(x): + """ + Converts number of days to a :class:`timdelta` object. + If *x* is a sequence, a sequence of :class:`timedelta` objects will + be returned. + + Parameters + ---------- + x : float, sequence of floats + Number of days (fraction part represents hours, minutes, seconds) + + Returns + ------- + :class:`timedelta` + + """ + if not cbook.iterable(x): + return _ordinalf_to_timedelta(x) + else: + x = np.asarray(x) + if not x.size: + return x + return _ordinalf_to_timedelta_np_vectorized(x).tolist() + + def drange(dstart, dend, delta): """ Return a date range as float Gregorian ordinals. *dstart* and diff --git a/lib/matplotlib/tests/test_dates.py b/lib/matplotlib/tests/test_dates.py index d87ba38edde2..5a25e6182b7e 100644 --- a/lib/matplotlib/tests/test_dates.py +++ b/lib/matplotlib/tests/test_dates.py @@ -457,3 +457,12 @@ def test_DayLocator(): def test_tz_utc(): dt = datetime.datetime(1970, 1, 1, tzinfo=mdates.UTC) dt.tzname() + + +@pytest.mark.parametrize("x, tdelta", + [(1, datetime.timedelta(days=1)), + ([1, 1.5], [datetime.timedelta(days=1), + datetime.timedelta(days=1.5)])]) +def test_num2timedelta(x, tdelta): + dt = mdates.num2timedelta(x) + assert dt == tdelta From 6dd8ef9934c5e29b2ed7cc7cdfce7e3e516d1a26 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Tue, 25 Jul 2017 13:19:28 +0100 Subject: [PATCH 2/2] Update docstring and add num2timedelta to __all__ --- lib/matplotlib/dates.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/dates.py b/lib/matplotlib/dates.py index 97216921d9b5..792ed69eb77a 100644 --- a/lib/matplotlib/dates.py +++ b/lib/matplotlib/dates.py @@ -136,7 +136,7 @@ import matplotlib.ticker as ticker -__all__ = ('date2num', 'num2date', 'drange', 'epoch2num', +__all__ = ('date2num', 'num2date', 'num2timedelta', 'drange', 'epoch2num', 'num2epoch', 'mx2num', 'DateFormatter', 'IndexDateFormatter', 'AutoDateFormatter', 'DateLocator', 'RRuleLocator', 'AutoDateLocator', 'YearLocator', @@ -426,7 +426,7 @@ def num2timedelta(x): Returns ------- - :class:`timedelta` + :class:`timedelta` or list[:class:`timedelta`] """ if not cbook.iterable(x): pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy