Skip to main content

Physical units interface to xarray using Pint

Project description

CI code coverage docs PyPI version codestyle conda-forge

pint-xarray

A convenience wrapper for using pint with xarray.

Usage

To convert the variables of a Dataset to quantities:

In [1]: import pint_xarray
   ...: import xarray as xr

In [2]: ds = xr.Dataset({"a": ("x", [0, 1, 2]), "b": ("y", [-3, 5, 1], {"units": "m"})})
   ...: ds
Out[2]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) int64 0 1 2
    b        (y) int64 -3 5 1

In [3]: q = ds.pint.quantify(a="s")
   ...: q
Out[3]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) int64 [s] 0 1 2
    b        (y) int64 [m] -3 5 1

to convert to different units:

In [4]: c = q.pint.to({"a": "ms", "b": "km"})
   ...: c
Out[4]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) float64 [ms] 0.0 1e+03 2e+03
    b        (y) float64 [km] -0.003 0.005 0.001

to convert back to non-quantities:

In [5]: d = c.pint.dequantify()
   ...: d
Out[5]:
<xarray.Dataset>
Dimensions:  (x: 3, y: 3)
Dimensions without coordinates: x, y
Data variables:
    a        (x) float64 0.0 1e+03 2e+03
    b        (y) float64 -0.003 0.005 0.001

For more, see the documentation

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

pint-xarray-0.3.tar.gz (47.5 kB view details)

Uploaded Source

Built Distribution

pint_xarray-0.3-py3-none-any.whl (32.3 kB view details)

Uploaded Python 3

File details

Details for the file pint-xarray-0.3.tar.gz.

File metadata

  • Download URL: pint-xarray-0.3.tar.gz
  • Upload date:
  • Size: 47.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for pint-xarray-0.3.tar.gz
Algorithm Hash digest
SHA256 3545dfa78bee3f98eba29b8bd17500e3b5cb7c7b03a2c2781c4d4d59b6a82841
MD5 684ef82d39d8b651654962d04bb0404a
BLAKE2b-256 e6e4ce34f50a2a5c8f13487c7e84f482389bfea6958e2c07cdde0fab12ec025a

See more details on using hashes here.

File details

Details for the file pint_xarray-0.3-py3-none-any.whl.

File metadata

  • Download URL: pint_xarray-0.3-py3-none-any.whl
  • Upload date:
  • Size: 32.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for pint_xarray-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a7d87c792a2e981cbff464bd1c875e872ef7a0c882a9395cfbc34512b3dcb1ab
MD5 e5def2573a7486d1686f60b59ff5dcfb
BLAKE2b-256 f22aca2d4ab154db0dc6f716e65a3c2d2f32a46e8ca8bd016962f517c779e57b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page