Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 95e5655

Browse filesBrowse files
authored
Merge pull request #8870 from dstansby/plot-timedelta
Add num2timedelta method with test
2 parents cfac3bd + 6dd8ef9 commit 95e5655
Copy full SHA for 95e5655

File tree

2 files changed

+42
-1
lines changed
Filter options

2 files changed

+42
-1
lines changed

‎lib/matplotlib/dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/dates.py
+33-1Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
import matplotlib.ticker as ticker
137137

138138

139-
__all__ = ('date2num', 'num2date', 'drange', 'epoch2num',
139+
__all__ = ('date2num', 'num2date', 'num2timedelta', 'drange', 'epoch2num',
140140
'num2epoch', 'mx2num', 'DateFormatter',
141141
'IndexDateFormatter', 'AutoDateFormatter', 'DateLocator',
142142
'RRuleLocator', 'AutoDateLocator', 'YearLocator',
@@ -406,6 +406,38 @@ def num2date(x, tz=None):
406406
return _from_ordinalf_np_vectorized(x, tz).tolist()
407407

408408

409+
def _ordinalf_to_timedelta(x):
410+
return datetime.timedelta(days=x)
411+
412+
413+
_ordinalf_to_timedelta_np_vectorized = np.vectorize(_ordinalf_to_timedelta)
414+
415+
416+
def num2timedelta(x):
417+
"""
418+
Converts number of days to a :class:`timdelta` object.
419+
If *x* is a sequence, a sequence of :class:`timedelta` objects will
420+
be returned.
421+
422+
Parameters
423+
----------
424+
x : float, sequence of floats
425+
Number of days (fraction part represents hours, minutes, seconds)
426+
427+
Returns
428+
-------
429+
:class:`timedelta` or list[:class:`timedelta`]
430+
431+
"""
432+
if not cbook.iterable(x):
433+
return _ordinalf_to_timedelta(x)
434+
else:
435+
x = np.asarray(x)
436+
if not x.size:
437+
return x
438+
return _ordinalf_to_timedelta_np_vectorized(x).tolist()
439+
440+
409441
def drange(dstart, dend, delta):
410442
"""
411443
Return a date range as float Gregorian ordinals. *dstart* and

‎lib/matplotlib/tests/test_dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_dates.py
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,3 +457,12 @@ def test_DayLocator():
457457
def test_tz_utc():
458458
dt = datetime.datetime(1970, 1, 1, tzinfo=mdates.UTC)
459459
dt.tzname()
460+
461+
462+
@pytest.mark.parametrize("x, tdelta",
463+
[(1, datetime.timedelta(days=1)),
464+
([1, 1.5], [datetime.timedelta(days=1),
465+
datetime.timedelta(days=1.5)])])
466+
def test_num2timedelta(x, tdelta):
467+
dt = mdates.num2timedelta(x)
468+
assert dt == tdelta

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.