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 5429f1c

Browse filesBrowse files
committed
Allow timedelta to be converted to an ordinalf
1 parent 0cce49b commit 5429f1c
Copy full SHA for 5429f1c

File tree

2 files changed

+18
-0
lines changed
Filter options

2 files changed

+18
-0
lines changed

‎lib/matplotlib/dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/dates.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ def _dt64_to_ordinalf(d):
271271
return dt
272272

273273

274+
def _tdelta_to_ordinalf(tdelta):
275+
"""
276+
Convert :mod:`timedelta` to total days. Return value is a :func:`float`
277+
"""
278+
return tdelta.total_seconds() / SEC_PER_DAY
279+
280+
281+
# a version of _tdelta_to_ordinalf that can operate on numpy arrays
282+
_tdelta_to_ordinalf_np_vectorized = np.vectorize(_tdelta_to_ordinalf)
283+
284+
274285
def _from_ordinalf(x, tz=None):
275286
"""
276287
Convert Gregorian float of the date, preserving hours, minutes,

‎lib/matplotlib/tests/test_dates.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_dates.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,3 +641,10 @@ def test_tz_utc():
641641
def test_num2timedelta(x, tdelta):
642642
dt = mdates.num2timedelta(x)
643643
assert dt == tdelta
644+
645+
646+
def test_timedelta_ordinalf():
647+
# Check that timedeltas can be converted to ordinalfs
648+
dt = datetime.timedelta(seconds=60)
649+
ordinalf = mdates._tdelta_to_ordinalf(dt)
650+
assert ordinalf == 1 / (24 * 60)

0 commit comments

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