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

Function infer_datetime_units

xarray/coding/times.py:771–788  ·  view source on GitHub ↗

Given an array of datetimes, returns a CF compatible time-unit string of the form "{time_unit} since {date[0]}", where `time_unit` is 'days', 'hours', 'minutes' or 'seconds' (the first one that can evenly divide all unique time deltas in `dates`)

(dates)

Source from the content-addressed store, hash-verified

769
770
771def infer_datetime_units(dates) -> str:
772 """Given an array of datetimes, returns a CF compatible time-unit string of
773 the form "{time_unit} since {date[0]}", where `time_unit` is 'days',
774 'hours', 'minutes' or 'seconds' (the first one that can evenly divide all
775 unique time deltas in `dates`)
776 """
777 dates = ravel(np.asarray(dates))
778 if np.issubdtype(np.asarray(dates).dtype, "datetime64"):
779 dates = to_datetime_unboxed(dates)
780 dates = dates[pd.notnull(dates)]
781 reference_date = dates[0] if len(dates) > 0 else "1970-01-01"
782 reference_date = pd.Timestamp(reference_date)
783 else:
784 reference_date = dates[0] if len(dates) > 0 else "1970-01-01"
785 reference_date = format_cftime_datetime(reference_date)
786 unique_timedeltas = np.unique(np.diff(dates))
787 units = _infer_time_units_from_diff(unique_timedeltas)
788 return f"{units} since {reference_date}"
789
790
791def format_cftime_datetime(date) -> str:

Calls 6

ravelFunction · 0.90
to_datetime_unboxedFunction · 0.85
format_cftime_datetimeFunction · 0.85
notnullMethod · 0.45
diffMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…