pint-xarray provides an interface between Pint (physical units library) and xarray (labeled multi-dimensional arrays). It enables attaching units to `xarray.DataArray` and `xarray.Dataset` objects, performing unit-aware computations, and ensuring dimensional consistency, making scientific data processing more robust. It is currently at version 0.6.1 and sees active development with releases typically tied to feature additions and bug fixes.
pip install pint-xarrayVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `xarray.DataArray` and `xarray.Dataset` objects with physical units using the `.pint` accessor. It shows how to quantify variables and perform unit conversions, highlighting the core functionality of unit-aware data handling.
Migrate code to use the `.pint.quantify()` method to explicitly attach units, and avoid direct manipulation of `attrs['units']` if unit-awareness is desired. Existing data saved before 0.5.0 might need re-quantifying upon loading.
Replace calls to `pint_xarray.convert_units(obj, unit)` with `obj.pint.to(unit)`.
Always ensure units are dimensionally compatible before an operation. Use `da.pint.to(target_unit)` or `da.pint.to_base_units()` to explicitly convert units if needed.
Always verify the units after complex operations, especially when chaining multiple xarray methods. If units are unexpectedly dropped or behave strangely, consider applying `.pint.quantify()` or `.pint.dequantify()` at critical points, or using `.pint.to()` to harmonize units before operations.
Add `import pint_xarray` at the beginning of your script or before you attempt to use the `.pint` accessor. The import statement itself registers the accessor.
Review the units of the involved DataArrays or Quantities. Ensure they are dimensionally compatible before performing the operation. If a conversion is intended, ensure the target unit is dimensionally consistent (e.g., `da.pint.to('kilometer')` for a meter-quantified array).