Skip to content

Commit 80a7cbd

Browse files
committed
Allow timedelta to be converted to an ordinalf
1 parent fe0095e commit 80a7cbd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/matplotlib/dates.py

+11
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ def _to_ordinalf(dt):
239239
_to_ordinalf_np_vectorized = np.vectorize(_to_ordinalf)
240240

241241

242+
def _tdelta_to_ordinalf(tdelta):
243+
"""
244+
Convert :mod:`timedelta` to total days. Return value is a :func:`float`
245+
"""
246+
return tdelta.total_seconds() / SEC_PER_DAY
247+
248+
249+
# a version of _tdelta_to_ordinalf that can operate on numpy arrays
250+
_tdelta_to_ordinalf_np_vectorized = np.vectorize(_tdelta_to_ordinalf)
251+
252+
242253
def _from_ordinalf(x, tz=None):
243254
"""
244255
Convert Gregorian float of the date, preserving hours, minutes,

lib/matplotlib/tests/test_dates.py

+7
Original file line numberDiff line numberDiff line change
@@ -474,3 +474,10 @@ def test_tz_utc():
474474
def test_num2timedelta(x, tdelta):
475475
dt = mdates.num2timedelta(x)
476476
assert dt == tdelta
477+
478+
479+
def test_timedelta_ordinalf():
480+
# Check that timedeltas can be converted to ordinalfs
481+
dt = datetime.timedelta(seconds=60)
482+
ordinalf = mdates._tdelta_to_ordinalf(dt)
483+
assert ordinalf == 1 / (24 * 60)

0 commit comments

Comments
 (0)