xarray extension for handling units
Project description
xarray-units
xarray extension for handling units
Overview
xarray-units is an import-only package that provides a DataArray accessor .units
for handling units such as converting units and numeric operations considering units.
Astropy is used as a backend.
Unlike similar implementations, xarray-units does not use a special data type to handle units, but uses the original data type in a DataArray.
This allows to continue to use powerful features such as parallel and lazy processing with Dask and/or user-defined DataArray subclasses.
Installation
pip install xarray-units==0.4.0
Basic usages
Suppose the following imports will be commonly used in the examples:
import xarray as xr
import xarray_units
Setting and unsetting units
xarray-units sets units in DataArray attributes (.attrs
) with the name "units"
:
da_km = xr.DataArray([1, 2, 3]).units.set("km")
print(da_km)
<xarray.DataArray (dim_0: 3)>
array([1, 2, 3])
Dimensions without coordinates: dim_0
Attributes:
units: km
And the units can also be unset (deleted):
da = da_km.units.unset()
print(da)
<xarray.DataArray (dim_0: 3)>
array([1, 2, 3])
Dimensions without coordinates: dim_0
These are equivalent to manually un/setting the units in the DataArray attributes, but the units
accessor also check that the units are valid when setting.
Converting units to others
xarray-units converts a DataArray with units to other units:
da_km = xr.DataArray([1, 2, 3]).units.set("km")
da_m = da_km.units.to("m")
print(da_m)
<xarray.DataArray (dim_0: 3)>
array([1000., 2000., 3000.])
Dimensions without coordinates: dim_0
Attributes:
units: m
Astropy equivalencies can also be used for equivalences between different units:
from astropy.units import spectral
da_mm = xr.DataArray([1, 2, 3]).units.set("mm")
da_GHz = da_mm.units.to("GHz", spectral())
print(da_GHz)
<xarray.DataArray (dim_1: 3)>
array([299.792458 , 149.896229 , 99.93081933])
Dimensions without coordinates: dim_0
Attributes:
units: GHz
Numeric operations considering units
xarray-units performs numerical operations considering units when the units
accessor is attached to the DataArray on the left side of the operator:
da_m = xr.DataArray([1000, 2000, 3000]).units.set("m")
da_km = xr.DataArray([1, 2, 3]).units.set("km")
da_sum_m = da_m.units + da_km
da_sum_km = da_km.units + da_m
print(da_sum_m)
print(da_sum_km)
<xarray.DataArray (dim_0: 3)>
array([2000., 4000., 6000.])
Dimensions without coordinates: dim_0
Attributes:
units: m
<xarray.DataArray (dim_0: 3)>
array([2., 4., 6.])
Dimensions without coordinates: dim_0
Attributes:
units: km
The units of the DataArray after the operation follows those of the DataArray with the units
accessor.
The resulting data values will be therefore different depending on the order of the operation.
They are, of course, equal when considering units:
da_eq = (da_sum_m.units == da_sum_km)
print(da_eq)
<xarray.DataArray (dim_0: 3)>
array([ True, True, True])
Dimensions without coordinates: dim_0
[!IMPORTANT] Because this feature is accessor-based, units are only considered for the operation right after the
units
accessor. See method and operation chains for performing multiple operations at once.
Formatting units
xarray-units converts units to various string formats:
da = xr.DataArray([1, 2, 3]).units.set("m / s^2")
da_console = da.units.format("console")
da_latex = da.units.format("latex")
print(da_console)
print(da_latex)
<xarray.DataArray (dim_0: 3)>
array([1, 2, 3])
Dimensions without coordinates: dim_0
Attributes:
units: m s^-2
<xarray.DataArray (dim_0: 3)>
array([1, 2, 3])
Dimensions without coordinates: dim_0
Attributes:
units: $\mathrm{\frac{m}{s^{2}}}$
This is useful, for example, when plotting a DataArray:
da.units.format("latex").plot()
Advanced usages
Method and operation chains
The units
accessor has an option for chaining methods or operations while considering units:
da_m = xr.DataArray([1, 2, 3]).units.set("m")
da_s = xr.DataArray([1, 2, 3]).units.set("s")
da_a = da_m.units(chain=2) / da_s / da_s
print(da_a)
<xarray.DataArray (dim_0: 3)>
array([1. , 0.5 , 0.33333333])
Dimensions without coordinates: dim_0
Attributes:
units: m / s2
where chain
is the number of chained methods or operations.
This is equivalent to nesting the units
accessors:
(da_m.units / da_s).units / da_s
Use with static type checking
xarray-units provides a special type hint xarray_units.DataArray
with which type checkers can statically handle the units
accessor and its methods:
from xarray_units import DataArray
da: DataArray = xr.DataArray([1, 2, 3]).units.set("km")
[!TIP]
xarray_units.DataArray
will be replaced byxarray.DataArray
at runtime, so it can also be used for creating and subclassingDataArray
.
Use without the units accessor
xarray-units provides a function xarray_units.units
that returns the units
accessor of a DataArray.
The following two codes are therefore equivalent:
xr.DataArray([1, 2, 3]).units.set("km")
from xarray_units import units
units(xr.DataArray([1, 2, 3])).set("km")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xarray_units-0.4.0.tar.gz
.
File metadata
- Download URL: xarray_units-0.4.0.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7158c73c124f797dd4760e8ab44adca49c09c70b5f4d8bb9953f0e4b91e81d6 |
|
MD5 | f00fb8b8ff1355cd21733e79a35d9f58 |
|
BLAKE2b-256 | 81e506af1a1f100c0f63d53797226ce888a9f8e9da6b8e499c883140a5614fe6 |
File details
Details for the file xarray_units-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: xarray_units-0.4.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.12.1 Linux/6.2.0-1019-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49599665ea0757a63fc107f79b12811c7bd0784fdf09ec187b7825c0ecf60483 |
|
MD5 | 3c180d3b7969990cbecae30be11db78c |
|
BLAKE2b-256 | 81679d3916b4df8c83383abf3e5ab494d1020a5565d9396932f20cb8d2520efe |