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.
Install & Compatibility
Where this runs
tested against v0.10.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 1.559s · 177.9MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 8.8s · import 1.474s · 171MB
180MB installed
● package 180MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Importing the top-level package registers the .cf accessor on xarray objects. No direct import of classes is usually needed for basic usage.
import cf_xarray
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.
import xarray as xr
import cf_xarray # This registers the .cf accessor
# Load a sample dataset (e.g., from xarray's tutorial data)
ds = xr.tutorial.load_dataset("air_temperature")
# Access CF-compliant coordinates
print("CF-identified coordinates:", ds.cf.coordinates)
print("Latitude variable name:", ds.cf["latitude"])
# Perform a mean operation using CF standard names
mean_temp = ds.air.cf.mean("latitude")
print("Mean temperature along latitude:\n", mean_temp)
# Add missing CF attributes (if necessary)
ds_incomplete = ds.copy(deep=True)
ds_incomplete.lat.attrs.pop('standard_name')
ds_incomplete.cf.guess_coord_axis(verbose=True)
print("Guessed coordinates for incomplete dataset:", ds_incomplete.cf.coordinates)
Upgrade
Version history
Breaking-change detection hasn't run for this library yet.
Audit
Security & dependencies
CVE tracking and dependency tree are planned for a later release.