MCPcopy Index your code
hub / github.com/pydata/xarray / timedelta_to_numeric

Function timedelta_to_numeric

xarray/core/duck_array_ops.py:679–712  ·  view source on GitHub ↗

Convert a timedelta-like object to numerical values. Parameters ---------- value : datetime.timedelta, numpy.timedelta64, pandas.Timedelta, str Time delta representation. datetime_unit : {Y, M, W, D, h, m, s, ms, us, ns, ps, fs, as} The time units of the output value

(value, datetime_unit="ns", dtype=float)

Source from the content-addressed store, hash-verified

677
678
679def timedelta_to_numeric(value, datetime_unit="ns", dtype=float):
680 """Convert a timedelta-like object to numerical values.
681
682 Parameters
683 ----------
684 value : datetime.timedelta, numpy.timedelta64, pandas.Timedelta, str
685 Time delta representation.
686 datetime_unit : {Y, M, W, D, h, m, s, ms, us, ns, ps, fs, as}
687 The time units of the output values. Note that some conversions are not allowed due to
688 non-linear relationships between units.
689 dtype : type
690 The output data type.
691
692 """
693 if isinstance(value, datetime.timedelta):
694 out = py_timedelta_to_float(value, datetime_unit)
695 elif isinstance(value, np.timedelta64):
696 out = np_timedelta64_to_float(value, datetime_unit)
697 elif isinstance(value, pd.Timedelta):
698 out = pd_timedelta_to_float(value, datetime_unit)
699 elif isinstance(value, str):
700 try:
701 a = pd.to_timedelta(value)
702 except ValueError as err:
703 raise ValueError(
704 f"Could not convert {value!r} to timedelta64 using pandas.to_timedelta"
705 ) from err
706 return py_timedelta_to_float(a, datetime_unit)
707 else:
708 raise TypeError(
709 f"Expected value of type str, pandas.Timedelta, datetime.timedelta "
710 f"or numpy.timedelta64, but received {type(value).__name__}"
711 )
712 return out.astype(dtype)
713
714
715def _to_pytimedelta(array, unit="us"):

Callers 2

interp_naFunction · 0.90

Calls 5

py_timedelta_to_floatFunction · 0.85
np_timedelta64_to_floatFunction · 0.85
pd_timedelta_to_floatFunction · 0.85
typeFunction · 0.85
astypeMethod · 0.45

Tested by 1

Used in the wild real call sites across dependent graphs

searching dependent graphs…