Convert an array containing datetime-like data to numerical values. Convert the datetime array to a timedelta relative to an offset. Parameters ---------- array : array-like Input data offset : None, datetime or cftime.datetime Datetime offset. If None, this is se
(array, offset=None, datetime_unit=None, dtype=float)
| 617 | |
| 618 | |
| 619 | def datetime_to_numeric(array, offset=None, datetime_unit=None, dtype=float): |
| 620 | """Convert an array containing datetime-like data to numerical values. |
| 621 | Convert the datetime array to a timedelta relative to an offset. |
| 622 | Parameters |
| 623 | ---------- |
| 624 | array : array-like |
| 625 | Input data |
| 626 | offset : None, datetime or cftime.datetime |
| 627 | Datetime offset. If None, this is set by default to the array's minimum |
| 628 | value to reduce round off errors. |
| 629 | datetime_unit : {None, Y, M, W, D, h, m, s, ms, us, ns, ps, fs, as} |
| 630 | If not None, convert output to a given datetime unit. Note that some |
| 631 | conversions are not allowed due to non-linear relationships between units. |
| 632 | dtype : dtype |
| 633 | Output dtype. |
| 634 | Returns |
| 635 | ------- |
| 636 | array |
| 637 | Numerical representation of datetime object relative to an offset. |
| 638 | Notes |
| 639 | ----- |
| 640 | Some datetime unit conversions won't work, for example from days to years, even |
| 641 | though some calendars would allow for them (e.g. no_leap). This is because there |
| 642 | is no `cftime.timedelta` object. |
| 643 | """ |
| 644 | # Set offset to minimum if not given |
| 645 | if offset is None: |
| 646 | if dtypes.is_datetime_like(array.dtype): |
| 647 | offset = _datetime_nanreduce(array, min) |
| 648 | else: |
| 649 | offset = min(array) |
| 650 | |
| 651 | # Compute timedelta object. |
| 652 | # For np.datetime64, this can silently yield garbage due to overflow. |
| 653 | # One option is to enforce 1970-01-01 as the universal offset. |
| 654 | |
| 655 | # This map_blocks call is for backwards compatibility. |
| 656 | # dask == 2021.04.1 does not support subtracting object arrays |
| 657 | # which is required for cftime |
| 658 | if is_duck_dask_array(array) and dtypes.is_object(array.dtype): |
| 659 | array = array.map_blocks(lambda a, b: a - b, offset, meta=array._meta) |
| 660 | else: |
| 661 | array = array - offset |
| 662 | |
| 663 | # Scalar is converted to 0d-array |
| 664 | if not hasattr(array, "dtype"): |
| 665 | array = np.array(array) |
| 666 | |
| 667 | # Convert timedelta objects to float by first converting to microseconds. |
| 668 | if dtypes.is_object(array.dtype): |
| 669 | return py_timedelta_to_float(array, datetime_unit or "ns").astype(dtype) |
| 670 | |
| 671 | # Convert np.NaT to np.nan |
| 672 | elif dtypes.is_datetime_like(array.dtype): |
| 673 | # Convert to specified timedelta units. |
| 674 | if datetime_unit: |
| 675 | array = array / np.timedelta64(1, datetime_unit) |
| 676 | return np.where(isnull(array), np.nan, array.astype(dtype)) |
no test coverage detected
searching dependent graphs…