{"id":7070,"library":"cf-xarray","title":"CF-Xarray","description":"CF-Xarray is a lightweight Python library that extends Xarray objects with an accessor (`.cf`) to interpret and utilize Climate and Forecast (CF) metadata conventions. It simplifies data analysis workflows by allowing users to refer to geophysical quantities by their standard CF names (e.g., 'latitude' instead of 'lat'), making code more generic across diverse CF-compliant datasets. The library is actively maintained as part of the `xarray-contrib` organization, with frequent releases.","status":"active","version":"0.10.11","language":"en","source_language":"en","source_url":"https://github.com/xarray-contrib/cf-xarray","tags":["xarray","scientific-computing","climate-data","netcdf","cf-conventions","geospatial"],"install":[{"cmd":"pip install cf-xarray","lang":"bash","label":"PyPI"}],"dependencies":[{"reason":"cf-xarray is an accessor for xarray DataArray and Dataset objects, providing CF-compliant functionality.","package":"xarray","optional":false},{"reason":"Required by xarray, which is a core dependency of cf-xarray.","package":"numpy","optional":false},{"reason":"Required by xarray, which is a core dependency of cf-xarray.","package":"pandas","optional":false},{"reason":"Required by xarray, which is a core dependency of cf-xarray.","package":"packaging","optional":false}],"imports":[{"note":"Importing the top-level package registers the .cf accessor on xarray objects. No direct import of classes is usually needed for basic usage.","symbol":"cf_xarray","correct":"import cf_xarray"}],"quickstart":{"code":"import xarray as xr\nimport cf_xarray # This registers the .cf accessor\n\n# Load a sample dataset (e.g., from xarray's tutorial data)\nds = xr.tutorial.load_dataset(\"air_temperature\")\n\n# Access CF-compliant coordinates\nprint(\"CF-identified coordinates:\", ds.cf.coordinates)\nprint(\"Latitude variable name:\", ds.cf[\"latitude\"])\n\n# Perform a mean operation using CF standard names\nmean_temp = ds.air.cf.mean(\"latitude\")\nprint(\"Mean temperature along latitude:\\n\", mean_temp)\n\n# Add missing CF attributes (if necessary)\nds_incomplete = ds.copy(deep=True)\nds_incomplete.lat.attrs.pop('standard_name')\nds_incomplete.cf.guess_coord_axis(verbose=True)\nprint(\"Guessed coordinates for incomplete dataset:\", ds_incomplete.cf.coordinates)","lang":"python","description":"Demonstrates loading an xarray dataset, accessing CF-compliant coordinates and axes via the `.cf` accessor, and performing an operation (mean) using CF standard names. It also shows how to use `guess_coord_axis()` for datasets with incomplete metadata."},"warnings":[{"fix":"Review xarray's documentation for attribute handling. If explicit attribute dropping or custom merging is needed, use `keep_attrs=False` or implement custom logic.","message":"Xarray's default attribute handling changed in versions >= 2025.11.0. Operations now preserve attributes by default and combine attributes from both operands using `drop_conflicts` in binary operations, instead of keeping only the left operand's attributes. This might affect workflows relying on previous attribute-dropping behavior.","severity":"breaking","affected_versions":"xarray >= 2025.11.0"},{"fix":"Inspect recognized attributes using `ds.cf.coordinates` or `ds.cf.axes`. Use methods like `ds.cf.guess_coord_axis()` or `ds.cf.add_canonical_attributes()` to infer or add missing metadata. Custom criteria can also be provided for identification.","message":"cf-xarray's utility is highly dependent on the presence and correctness of CF-compliant metadata (attributes) in your xarray objects. If metadata is incomplete or non-standard, `cf-xarray` might not identify variables as expected.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware of the limitations for non-rectilinear grids. For critical applications, prefer datasets with pre-calculated, explicitly provided bounds or grid metrics from the data producer.","message":"The `cf.add_bounds()` method estimates coordinate bounds using linear interpolation and extrapolation. While effective for rectilinear grids, this can be a coarse approximation for curvilinear or irregular grids, potentially leading to inaccuracies or unmatching corners in complex geometries.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your environment uses Python 3.11 or later and the latest compatible version of xarray.","message":"Xarray, cf-xarray's core dependency, dropped support for Python 2.7 in `xarray` v0.12.0. cf-xarray itself requires Python >=3.11.","severity":"deprecated","affected_versions":"xarray < 0.12.0 (and Python < 3.11 for cf-xarray)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Add `import cf_xarray` at the beginning of your script or before any `.cf` accessor calls.","cause":"The `cf-xarray` accessor has not been registered with xarray. This typically happens if `import cf_xarray` is omitted or executed after the xarray object is created and accessed.","error":"AttributeError: 'DataArray' object has no attribute 'cf'"},{"fix":"Inspect `ds.cf.coordinates` or `ds.cf.axes` to see which CF attributes are recognized. You might need to manually add appropriate attributes to your xarray object (e.g., `da.attrs['standard_name'] = 'latitude'`) or use `ds.cf.guess_coord_axis()` to infer them.","cause":"The xarray Dataset or DataArray lacks the necessary CF-compliant attributes (e.g., 'standard_name', 'axis', 'units') for cf-xarray to identify a variable as 'latitude'.","error":"KeyError: 'latitude' (or similar CF standard name) when using ds.cf['latitude']"},{"fix":"Install xarray: `pip install xarray cf-xarray`.","cause":"xarray is a mandatory dependency for cf-xarray, and it is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'xarray'"}]}