|
136 | 136 | import matplotlib.ticker as ticker
|
137 | 137 |
|
138 | 138 |
|
139 |
| -__all__ = ('date2num', 'num2date', 'drange', 'epoch2num', |
| 139 | +__all__ = ('date2num', 'num2date', 'num2timedelta', 'drange', 'epoch2num', |
140 | 140 | 'num2epoch', 'mx2num', 'DateFormatter',
|
141 | 141 | 'IndexDateFormatter', 'AutoDateFormatter', 'DateLocator',
|
142 | 142 | 'RRuleLocator', 'AutoDateLocator', 'YearLocator',
|
@@ -406,6 +406,38 @@ def num2date(x, tz=None):
|
406 | 406 | return _from_ordinalf_np_vectorized(x, tz).tolist()
|
407 | 407 |
|
408 | 408 |
|
| 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 | + |
409 | 441 | def drange(dstart, dend, delta):
|
410 | 442 | """
|
411 | 443 | Return a date range as float Gregorian ordinals. *dstart* and
|
|
0 commit comments