Coverage for stems/times.py : 59%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Module for handling dates and times """
# ----------------------------------------------------------------------------- def ordinal_to_datetime64(x): """ Convert ordinal to datetime64, handling NaN/NaT """ raise TypeError(f'Not supported for type "{type(x)}"')
def _ordinal_to_datetime64_scalar(x): if x > 0: return np.datetime64(dt.datetime.fromordinal(x)) else: return np.datetime64('NaT')
def _ordinal_to_datetime64_sequence(x): x_ = [_ordinal_to_datetime64_scalar(x_) for x_ in x] return type(x)(x_)
def _ordinal_to_datetime64_np(x): if x.ndim == 0: return _ordinal_to_datetime64_scalar(x.item()) else: return np.array([_ordinal_to_datetime64_scalar(x_) for x_ in x], dtype=np.datetime64)
def _ordinal_to_datetime64_pd(x): x_ = _ordinal_to_datetime64_np(x.values) if isinstance(x, pd.Index): return pd.Index(x_, name=x.name) else: return pd.Series(x_, name=x.name)
def _ordinal_to_datetime64_da(x): return da.map_blocks(_ordinal_to_datetime64_np, x, )
def _ordinal_to_datetime64_xarr(x): x_ = _ordinal_to_datetime64_array(x.data) return xr.DataArray(x_, dims=x.dims, coords=x.coords, attrs=x.attrs, name=x.name)
# ----------------------------------------------------------------------------- def datetime64_to_pydatetime(x): """ Convert datetime64 to Python datetime.datetime """ raise TypeError(f'Not supported for type "{type(x)}"')
def _datetime64_to_pydatetime_array(x): return x.astype('M8[ms]').astype('O')
def _datetime64_to_pydatetime_xarray(x): x_ = _datetime64_to_pydatetime_array(x.data) return xr.DataArray(x_, dims=x.dims, coords=x.coords, attrs=x.attrs, name=x.name)
# ----------------------------------------------------------------------------- """ Convert datetime to ordinal """ raise TypeError(f'Not supported for type "{type(x)}"')
def _datetime64_to_ordinal_list(x): x_ = [_datetime64_to_ordinal_scalar(x_) for x_ in x] return type(x)(x_)
def _datetime64_to_ordinal_np(x): # Convert to pydatetime first if datetime if np.issubdtype(x.dtype, np.datetime64): x = datetime64_to_pydatetime(x) if x.ndim == 0: return np.array(x.item().toordinal()) else: return np.array([x_.toordinal() for x_ in x])
x_ = _datetime64_to_ordinal_np(x.values) if isinstance(x, pd.Index): return pd.Index(x_, name=name or x.name) else: return pd.Series(x_, name=name or x.name)
def _datetime64_to_ordinal_da(x): return da.map_blocks(_datetime64_to_ordinal_np, x, dtype=np.int)
x_ = datetime64_to_ordinal(x.data) return xr.DataArray(x_, dims=x.dims, coords=x.coords, attrs=x.attrs, name=name or x.name)
# ----------------------------------------------------------------------------- """ Convert time data to some string format (e.g., 20000101) Parameters ---------- time : array-like Array of time data strf : str, optional String format to be used with ``strftime`` cast : callable Function to cast the string format to a number fill : int or float Fill value to use when ``time`` is NaN or NaT Returns ------- array-like Ordinal date formatted into a date string numeric representation """ raise TypeError('`time` must be a NumPy, Dask, or Xarray array')
fill=-9999): # to_datetime needs 1D, so reshape after func
# NaN or NaT in `time_` is now 'NaT' in strf_time_, so we need to fill it
# Now it's safe to cast to numeric
fill=-9999): dtype=cast, strf=strf, cast=cast, fill=fill)
fill=-9999): name=time.name, attrs=time.attrs) |