Convert datetime objects to Matplotlib dates. Parameters ---------- d : `datetime.datetime` or `numpy.datetime64` or sequences of these Returns ------- float or sequence of floats Number of days since the epoch. See `.get_epoch` for the epoch, which ca
(d)
| 401 | |
| 402 | |
| 403 | def date2num(d): |
| 404 | """ |
| 405 | Convert datetime objects to Matplotlib dates. |
| 406 | |
| 407 | Parameters |
| 408 | ---------- |
| 409 | d : `datetime.datetime` or `numpy.datetime64` or sequences of these |
| 410 | |
| 411 | Returns |
| 412 | ------- |
| 413 | float or sequence of floats |
| 414 | Number of days since the epoch. See `.get_epoch` for the |
| 415 | epoch, which can be changed by :rc:`date.epoch` or `.set_epoch`. If |
| 416 | the epoch is "1970-01-01T00:00:00" (default) then noon Jan 1 1970 |
| 417 | ("1970-01-01T12:00:00") returns 0.5. |
| 418 | |
| 419 | Notes |
| 420 | ----- |
| 421 | The Gregorian calendar is assumed; this is not universal practice. |
| 422 | For details see the module docstring. |
| 423 | """ |
| 424 | # Unpack in case of e.g. Pandas or xarray object |
| 425 | d = cbook._unpack_to_numpy(d) |
| 426 | |
| 427 | # make an iterable, but save state to unpack later: |
| 428 | iterable = np.iterable(d) |
| 429 | if not iterable: |
| 430 | d = [d] |
| 431 | |
| 432 | masked = np.ma.is_masked(d) |
| 433 | mask = np.ma.getmask(d) |
| 434 | d = np.asarray(d) |
| 435 | |
| 436 | # convert to datetime64 arrays, if not already: |
| 437 | if not np.issubdtype(d.dtype, np.datetime64): |
| 438 | # datetime arrays |
| 439 | if not d.size: |
| 440 | # deals with an empty array... |
| 441 | return d |
| 442 | tzi = getattr(d[0], 'tzinfo', None) |
| 443 | if tzi is not None: |
| 444 | # make datetime naive: |
| 445 | d = [dt.astimezone(UTC).replace(tzinfo=None) for dt in d] |
| 446 | d = np.asarray(d) |
| 447 | d = d.astype('datetime64[us]') |
| 448 | |
| 449 | d = np.ma.masked_array(d, mask=mask) if masked else d |
| 450 | d = _dt64_to_ordinalf(d) |
| 451 | |
| 452 | return d if iterable else d[0] |
| 453 | |
| 454 | |
| 455 | def num2date(x, tz=None): |
no test coverage detected
searching dependent graphs…