Registry / data / cf-xarray

cf-xarray

JSON →
library0.10.11pypypi✓ verified 35d ago

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.

data
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
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 1.559s · 177.9MB
glibc
py 3.103.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)
Debug
Known footguns
breakingXarray'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.
gotchacf-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.
gotchaThe `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.
deprecatedXarray, cf-xarray's core dependency, dropped support for Python 2.7 in `xarray` v0.12.0. cf-xarray itself requires Python >=3.11.
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.

Agent activity
19 hits · last 30 days
bytedance
6
gptbot
4
ahrefsbot
3
script
1
Resources