xarray.decode_cf#

xarray.decode_cf(obj, concat_characters=True, mask_and_scale=True, decode_times=True, decode_coords=True, drop_variables=None, use_cftime=None, decode_timedelta=None)[source]#

Decode the given Dataset or Datastore according to CF conventions.

The result is a new Dataset.

Parameters:
  • obj (Dataset or DataStore) – Object to decode.

  • concat_characters (bool, optional) – Should character arrays be concatenated to strings, for example: [“h”, “e”, “l”, “l”, “o”] -> “hello”

  • mask_and_scale (bool, optional) – Lazily scale (using scale_factor and add_offset) and mask (using _FillValue).

  • decode_times (bool | CFDatetimeCoder | Mapping[str, bool | CFDatetimeCoder], optional) – Decode cf times (e.g., integers since “hours since 2000-01-01”) to np.datetime64.

  • decode_coords (bool or {"coordinates", "all"}, optional) – Controls which variables are set as coordinate variables:

    • “coordinates” or True: Set variables referred to in the 'coordinates' attribute of the datasets or individual variables as coordinate variables.

    • “all”: Set variables referred to in 'grid_mapping', 'bounds' and other attributes as coordinate variables.

  • drop_variables (str or iterable, optional) – A variable or list of variables to exclude from being parsed from the dataset. This may be useful to drop variables with problems or inconsistent values.

  • 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[ns] 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[ns] objects. If False, always decode times to np.datetime64[ns] objects; if this is not possible raise an error.

    Deprecated since version 2025.01.1: Please pass a coders.CFDatetimeCoder instance initialized with use_cftime to the decode_times kwarg instead.

  • decode_timedelta (bool | CFTimedeltaCoder | Mapping[str, bool | CFTimedeltaCoder], optional) – If True or CFTimedeltaCoder, decode variables and coordinates with time units in {“days”, “hours”, “minutes”, “seconds”, “milliseconds”, “microseconds”} into timedelta objects. If False, leave them encoded as numbers. If None (default), assume the same behavior as decode_times. The resolution of the decoded timedeltas can be configured with the time_unit argument in the CFTimedeltaCoder passed.

Returns:

decoded (Dataset)

See also

open_dataset

Examples

Decode times and apply the scale factor of a dataset with CF attributes:

>>> ds = xr.Dataset(
...     {
...         "temp": (
...             "time",
...             np.array([10, 11, 12], dtype="int16"),
...             {"scale_factor": 0.5},
...         )
...     },
...     coords={
...         "time": ("time", [0.0, 1.0, 2.0], {"units": "days since 2000-01-01"})
...     },
... )
>>> decoded = xr.decode_cf(ds)
>>> decoded
<xarray.Dataset> Size: 48B
Dimensions:  (time: 3)
Coordinates:
  * time     (time) datetime64[ns] 24B 2000-01-01 2000-01-02 2000-01-03
Data variables:
    temp     (time) float64 24B ...
>>> decoded["temp"].values
array([5. , 5.5, 6. ])

Each decoding step can be turned off, e.g. to keep the raw values:

>>> xr.decode_cf(ds, mask_and_scale=False, decode_times=False)
<xarray.Dataset> Size: 30B
Dimensions:  (time: 3)
Coordinates:
  * time     (time) float64 24B 0.0 1.0 2.0
Data variables:
    temp     (time) int16 6B 10 11 12