xarray.coding.times.CFDatetimeCoder#

class xarray.coding.times.CFDatetimeCoder(use_cftime=None, time_unit='ns', infer_dtype='data')[source]#

Coder for CF Datetime coding.

Parameters:
  • use_cftime (bool, optional) – Only relevant if encoded dates come from a standard calendar (e.g. “gregorian”, “proleptic_gregorian”, “standard”, or not specified). If None (default), attempt to decode times to np.datetime64 objects; if this is not possible, decode times to cftime.datetime objects. If True, always decode times to cftime.datetime objects, regardless of whether or not they can be represented using np.datetime64 objects. If False, always decode times to np.datetime64 objects; if this is not possible raise an error. May not be supported by all the backends.

  • time_unit (PDDatetimeUnitOptions) – Target resolution when decoding dates. Defaults to “ns”.

  • infer_dtype ({"data", "metadata"}, default: "data") – How to infer the dtype of the decoded variable:

    • "data" (default): read the first and last values from the store to verify they decode successfully. Preserves backwards-compatible behavior, including the auto-fallback to cftime.datetime for values outside the np.datetime64[ns] range.

    • "metadata": infer the dtype from units, calendar, use_cftime and time_unit alone, assuming the stored dates lie near the reference date in units. Nothing is read from the store, which speeds up opening remote stores with many time-encoded variables. The first and last values are read, as with "data", only when metadata cannot tell: when, with use_cftime=None and a standard calendar, dates near the reference date need cftime (e.g. "hours since 1-1-1" or a reference date before 1582-10-15), or when decoding dates near the reference date fails.

      The stored values are checked when they are decoded instead, and a ValueError is raised then if they need a different dtype than inferred. This happens for dates outside the np.datetime64[time_unit] range (pass use_cftime=True or a coarser time_unit such as "s") and for floating point values with fractions finer than units, e.g. 0.5 with "seconds since ..." (pass a time_unit fine enough to represent them). Index coordinates are decoded when the dataset is opened, other variables when they are loaded.

__init__(use_cftime=None, time_unit='ns', infer_dtype='data')[source]#

Methods

__init__([use_cftime, time_unit, infer_dtype])

decode(variable[, name])

Convert a decoded variable to an encoded variable.

encode(variable[, name])

Convert an encoded variable to a decoded variable.