File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -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`
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+
409441def drange (dstart , dend , delta ):
410442 """
411443 Return a date range as float Gregorian ordinals. *dstart* and
Original file line number Diff line number Diff line change @@ -457,3 +457,12 @@ def test_DayLocator():
457457def 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
You can’t perform that action at this time.
0 commit comments