Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a79f336

Browse files
committed
Allow timedelta to be converted to an ordinalf
1 parent ebac433 commit a79f336

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/matplotlib/dates.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,17 @@ def _dt64_to_ordinalf(d):
294294
return dt
295295

296296

297+
def _tdelta_to_ordinalf(tdelta):
298+
"""
299+
Convert :mod:`timedelta` to total days. Return value is a :func:`float`
300+
"""
301+
return tdelta.total_seconds() / SEC_PER_DAY
302+
303+
304+
# a version of _tdelta_to_ordinalf that can operate on numpy arrays
305+
_tdelta_to_ordinalf_np_vectorized = np.vectorize(_tdelta_to_ordinalf)
306+
307+
297308
def _from_ordinalf(x, tz=None):
298309
"""
299310
Convert Gregorian float of the date, preserving hours, minutes,

lib/matplotlib/tests/test_dates.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,3 +612,10 @@ def test_tz_utc():
612612
def test_num2timedelta(x, tdelta):
613613
dt = mdates.num2timedelta(x)
614614
assert dt == tdelta
615+
616+
617+
def test_timedelta_ordinalf():
618+
# Check that timedeltas can be converted to ordinalfs
619+
dt = datetime.timedelta(seconds=60)
620+
ordinalf = mdates._tdelta_to_ordinalf(dt)
621+
assert ordinalf == 1 / (24 * 60)

0 commit comments

Comments
 (0)