Xarray (pronounced 'ex-array') is an open-source Python package that simplifies working with labelled multi-dimensional arrays and datasets. It introduces labels in the form of dimensions, coordinates, and attributes on top of raw NumPy-like arrays, enabling a more intuitive and less error-prone experience for scientific computing and data analysis, particularly for earth sciences. As of February 2026, the current version is 2026.2.0. Xarray maintains a regular release cadence, with minor versions typically released monthly or bi-monthly.
pip install xarrayVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the creation of a basic `DataArray` and a `Dataset`, including dimension names, coordinates, and attributes. It then shows how to perform a simple aggregation (mean) on a variable within the `Dataset` along a specified dimension.
Review code that relies on attributes being dropped by default. If the old behavior is desired, explicitly set `keep_attrs=False` or manually manage attributes.
Explicitly convert the `DataArray` to a NumPy array using `.values` before applying the NumPy ufunc (e.g., `np.add.reduce(da.values)`).
If only data identity is required, compare the underlying data (`.values` or by casting to `pandas.DataFrame`). Adjust identity checks if index differences are now considered relevant.
Ensure that the length of each coordinate provided for a dimension precisely matches the actual size of that dimension in the input data or dataset being constructed. For example, if data has shape `(3, 2)` for dimensions `('lat', 'lon')`, then `coords['lat']` must have length `3` and `coords['lon']` must have length `2`.To resolve this, install the HDF5 development package on your Alpine system (e.g., `apk add hdf5-dev`) before attempting to install 'netCDF4' or xarray with netCDF4 as a dependency.
Install the appropriate backend library using `pip install netcdf4` (or `h5netcdf`, `zarr`, `cfgrib`, etc., as needed for your file type).
Ensure dimensions and coordinates align appropriately, or use `xarray.concat()` with `coords='minimal'` or `compat='override'` if conflicts are expected, or explicitly select/rename variables/dimensions before merging.
Verify the exact name of the coordinate or variable using `ds.coords`, `ds.data_vars`, or `ds.dims` (for Dataset) or `da.coords` (for DataArray) and use the correct name in your selection.
Ensure operands have compatible numeric data types using `.astype()`, explicitly align dimensions using `.align()`, or compute Dask arrays to concrete NumPy arrays using `.compute()` if the operation requires it.