Skip to content

Commit 62c15c5

Browse files
TomNicholaskeewis
andauthored
parse_units instead of parse_expression (#41)
* test for interpreting inverse integer unit * replace parse_expression with parse_units * black formatting * updated what's new * moved test * black reformatting Co-authored-by: keewis <[email protected]> * rerun black Co-authored-by: keewis <[email protected]> Co-authored-by: Keewis <[email protected]>
1 parent 993c8df commit 62c15c5

File tree

4 files changed

+15
-25
lines changed

4 files changed

+15
-25
lines changed

docs/whats-new.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ What's new
33

44
0.2 (*unreleased*)
55
------------------
6-
6+
- rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`,
7+
to use pint's `parse_units` instead of `parse_expression` (:pull:`40`)
78

89
v0.1 (October 26 2020)
910
----------------------

pint_xarray/accessors.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def _decide_units(units, registry, unit_attribute):
132132
raise ValueError("no units given")
133133
elif units is None:
134134
# TODO option to read and decode units according to CF conventions (see MetPy)?
135-
units = registry.parse_expression(unit_attribute).units
135+
units = registry.parse_units(unit_attribute)
136136
elif isinstance(units, Unit):
137137
# TODO do we have to check what happens if someone passes a Unit instance
138138
# without creating a unit registry?
@@ -227,11 +227,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
227227

228228
units = either_dict_or_kwargs(units, unit_kwargs, ".quantify")
229229

230-
registry = get_registry(
231-
unit_registry,
232-
units,
233-
conversion.extract_units(self.da),
234-
)
230+
registry = get_registry(unit_registry, units, conversion.extract_units(self.da))
235231

236232
unit_attrs = conversion.extract_unit_attributes(self.da)
237233
new_obj = conversion.strip_unit_attributes(self.da)
@@ -266,8 +262,7 @@ def dequantify(self):
266262

267263
units = units_to_str_or_none(conversion.extract_units(self.da))
268264
new_obj = conversion.attach_unit_attributes(
269-
conversion.strip_units(self.da),
270-
units,
265+
conversion.strip_units(self.da), units
271266
)
272267

273268
return new_obj
@@ -498,11 +493,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
498493
b (x) int64 <Quantity([ 5 -2 1], 'decimeter')>
499494
"""
500495
units = either_dict_or_kwargs(units, unit_kwargs, ".quantify")
501-
registry = get_registry(
502-
unit_registry,
503-
units,
504-
conversion.extract_units(self.ds),
505-
)
496+
registry = get_registry(unit_registry, units, conversion.extract_units(self.ds))
506497

507498
unit_attrs = conversion.extract_unit_attributes(self.ds)
508499
new_obj = conversion.strip_unit_attributes(self.ds)

pint_xarray/tests/test_accessors.py

+6
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def test_error_on_nonsense_units(self, example_unitless_da):
9494
with pytest.raises(UndefinedUnitError):
9595
da.pint.quantify(units="aecjhbav")
9696

97+
def test_parse_integer_inverse(self):
98+
# Regression test for issue #40
99+
da = xr.DataArray([10], attrs={"units": "m^-1"})
100+
result = da.pint.quantify()
101+
assert result.pint.units == Unit("1 / meter")
102+
97103

98104
class TestDequantifyDataArray:
99105
def test_strip_units(self, example_quantity_da):

pint_xarray/tests/test_conversion.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,7 @@ def test_attach_units(self, obj, units):
196196
{"a": "K", "b": "hPa", "u": "m"},
197197
id="Dataset",
198198
),
199-
pytest.param(
200-
Variable("x", []),
201-
{None: "hPa"},
202-
id="Variable",
203-
),
199+
pytest.param(Variable("x", []), {None: "hPa"}, id="Variable"),
204200
),
205201
)
206202
def test_attach_unit_attributes(self, obj, units):
@@ -370,9 +366,7 @@ def test_extract_units(self, typename, units):
370366
id="Dataset",
371367
),
372368
pytest.param(
373-
Variable("x", [], {"units": "hPa"}),
374-
{None: "hPa"},
375-
id="Variable",
369+
Variable("x", [], {"units": "hPa"}), {None: "hPa"}, id="Variable"
376370
),
377371
),
378372
)
@@ -446,9 +440,7 @@ def test_strip_units(self, obj):
446440
id="Dataset",
447441
),
448442
pytest.param(
449-
Variable("x", [], {"units": "hPa"}),
450-
{None: "hPa"},
451-
id="Variable",
443+
Variable("x", [], {"units": "hPa"}), {None: "hPa"}, id="Variable"
452444
),
453445
),
454446
)

0 commit comments

Comments
 (0)