Skip to content

Commit

Permalink
parse_units instead of parse_expression (#41)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Nov 1, 2020
1 parent 993c8df commit 62c15c5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
3 changes: 2 additions & 1 deletion docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ What's new

0.2 (*unreleased*)
------------------

- rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`,
to use pint's `parse_units` instead of `parse_expression` (:pull:`40`)

v0.1 (October 26 2020)
----------------------
Expand Down
17 changes: 4 additions & 13 deletions pint_xarray/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _decide_units(units, registry, unit_attribute):
raise ValueError("no units given")
elif units is None:
# TODO option to read and decode units according to CF conventions (see MetPy)?
units = registry.parse_expression(unit_attribute).units
units = registry.parse_units(unit_attribute)
elif isinstance(units, Unit):
# TODO do we have to check what happens if someone passes a Unit instance
# without creating a unit registry?
Expand Down Expand Up @@ -227,11 +227,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):

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

registry = get_registry(
unit_registry,
units,
conversion.extract_units(self.da),
)
registry = get_registry(unit_registry, units, conversion.extract_units(self.da))

unit_attrs = conversion.extract_unit_attributes(self.da)
new_obj = conversion.strip_unit_attributes(self.da)
Expand Down Expand Up @@ -266,8 +262,7 @@ def dequantify(self):

units = units_to_str_or_none(conversion.extract_units(self.da))
new_obj = conversion.attach_unit_attributes(
conversion.strip_units(self.da),
units,
conversion.strip_units(self.da), units
)

return new_obj
Expand Down Expand Up @@ -498,11 +493,7 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
b (x) int64 <Quantity([ 5 -2 1], 'decimeter')>
"""
units = either_dict_or_kwargs(units, unit_kwargs, ".quantify")
registry = get_registry(
unit_registry,
units,
conversion.extract_units(self.ds),
)
registry = get_registry(unit_registry, units, conversion.extract_units(self.ds))

unit_attrs = conversion.extract_unit_attributes(self.ds)
new_obj = conversion.strip_unit_attributes(self.ds)
Expand Down
6 changes: 6 additions & 0 deletions pint_xarray/tests/test_accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def test_error_on_nonsense_units(self, example_unitless_da):
with pytest.raises(UndefinedUnitError):
da.pint.quantify(units="aecjhbav")

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


class TestDequantifyDataArray:
def test_strip_units(self, example_quantity_da):
Expand Down
14 changes: 3 additions & 11 deletions pint_xarray/tests/test_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,7 @@ def test_attach_units(self, obj, units):
{"a": "K", "b": "hPa", "u": "m"},
id="Dataset",
),
pytest.param(
Variable("x", []),
{None: "hPa"},
id="Variable",
),
pytest.param(Variable("x", []), {None: "hPa"}, id="Variable"),
),
)
def test_attach_unit_attributes(self, obj, units):
Expand Down Expand Up @@ -370,9 +366,7 @@ def test_extract_units(self, typename, units):
id="Dataset",
),
pytest.param(
Variable("x", [], {"units": "hPa"}),
{None: "hPa"},
id="Variable",
Variable("x", [], {"units": "hPa"}), {None: "hPa"}, id="Variable"
),
),
)
Expand Down Expand Up @@ -446,9 +440,7 @@ def test_strip_units(self, obj):
id="Dataset",
),
pytest.param(
Variable("x", [], {"units": "hPa"}),
{None: "hPa"},
id="Variable",
Variable("x", [], {"units": "hPa"}), {None: "hPa"}, id="Variable"
),
),
)
Expand Down

0 comments on commit 62c15c5

Please sign in to comment.