Skip to content

Commit 2c8a27a

Browse files
authored
improve the documentation of the format parameter of .pint.dequantify (#127)
* update the parameter description and add examples * update whats-new.rst * update the PR number * display the result of .dequantify variable by variable
1 parent a09dd05 commit 2c8a27a

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

docs/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ What's new
1111
- convert the note about dimension coordinates saving their units in the attributes a
1212
warning (:issue:`124`, :pull:`126`)
1313
By `Justus Magin <https://github.com/keewis>`_.
14+
- improve the documentation on the ``format`` parameter of :py:meth:`Dataset.pint.dequantify`
15+
and :py:meth:`DataArray.pint.dequantify` (:issue:`121`, :pull:`127`)
16+
By `Justus Magin <https://github.com/keewis>`_.
1417
- use ``cf-xarray``'s unit registry in the plotting example (:issue:`107`, :pull:`128`).
1518
By `Justus Magin <https://github.com/keewis>`_.
1619

pint_xarray/accessors.py

+73-2
Original file line numberDiff line numberDiff line change
@@ -375,13 +375,39 @@ def dequantify(self, format=None):
375375
Parameters
376376
----------
377377
format : str, optional
378-
The format used for the string representations.
378+
The format specification (as accepted by pint) used for the string
379+
representations.
379380
380381
Returns
381382
-------
382383
dequantified : DataArray
383384
DataArray whose array data is unitless, and of the type
384385
that was previously wrapped by `pint.Quantity`.
386+
387+
See Also
388+
--------
389+
https://pint.readthedocs.io/en/stable/tutorial.html#string-formatting
390+
391+
Examples
392+
--------
393+
>>> da = xr.DataArray([0, 1], dims="x")
394+
>>> q = da.pint.quantify("m")
395+
>>> q
396+
<xarray.DataArray (x: 2)>
397+
<Quantity([0 1], 'meter')>
398+
Dimensions without coordinates: x
399+
>>> q.pint.dequantify(format="P")
400+
<xarray.DataArray (x: 2)>
401+
array([0, 1])
402+
Dimensions without coordinates: x
403+
Attributes:
404+
units: meter
405+
>>> q.pint.dequantify(format="~P")
406+
<xarray.DataArray (x: 2)>
407+
array([0, 1])
408+
Dimensions without coordinates: x
409+
Attributes:
410+
units: m
385411
"""
386412
units = conversion.extract_unit_attributes(self.da)
387413
units.update(conversion.extract_units(self.da))
@@ -1027,13 +1053,58 @@ def dequantify(self, format=None):
10271053
Parameters
10281054
----------
10291055
format : str, optional
1030-
The format used for the string representations.
1056+
The format specification (as accepted by pint) used for the string
1057+
representations.
10311058
10321059
Returns
10331060
-------
10341061
dequantified : Dataset
10351062
Dataset whose data variables are unitless, and of the type
10361063
that was previously wrapped by ``pint.Quantity``.
1064+
1065+
See Also
1066+
--------
1067+
https://pint.readthedocs.io/en/stable/tutorial.html#string-formatting
1068+
1069+
Examples
1070+
--------
1071+
>>> ds = xr.Dataset({"a": ("x", [0, 1]), "b": ("y", [2, 3, 4])})
1072+
>>> q = ds.pint.quantify({"a": "m", "b": "s"})
1073+
>>> q
1074+
<xarray.Dataset>
1075+
Dimensions: (x: 2, y: 3)
1076+
Dimensions without coordinates: x, y
1077+
Data variables:
1078+
a (x) int64 [m] 0 1
1079+
b (y) int64 [s] 2 3 4
1080+
1081+
>>> d = q.pint.dequantify(format="P")
1082+
>>> d.a
1083+
<xarray.DataArray 'a' (x: 2)>
1084+
array([0, 1])
1085+
Dimensions without coordinates: x
1086+
Attributes:
1087+
units: meter
1088+
>>> d.b
1089+
<xarray.DataArray 'b' (y: 3)>
1090+
array([2, 3, 4])
1091+
Dimensions without coordinates: y
1092+
Attributes:
1093+
units: second
1094+
1095+
>>> d = q.pint.dequantify(format="~P")
1096+
>>> d.a
1097+
<xarray.DataArray 'a' (x: 2)>
1098+
array([0, 1])
1099+
Dimensions without coordinates: x
1100+
Attributes:
1101+
units: m
1102+
>>> d.b
1103+
<xarray.DataArray 'b' (y: 3)>
1104+
array([2, 3, 4])
1105+
Dimensions without coordinates: y
1106+
Attributes:
1107+
units: s
10371108
"""
10381109
units = conversion.extract_unit_attributes(self.ds)
10391110
units.update(conversion.extract_units(self.ds))

0 commit comments

Comments
 (0)