Skip to content

Commit 7518c84

Browse files
TomNicholaskeewis
andauthored
Give quantify errors context (#43)
* unpack list comprehension * raise from to give context * re-raise same type of error * added test * black formatting * updated what's new * Raised more general ValueError instead * update tests to match new error types * linting * Update docs/whats-new.rst Co-authored-by: keewis <[email protected]> * use pytest raises with match Co-authored-by: keewis <[email protected]> Co-authored-by: keewis <[email protected]>
1 parent ddb3624 commit 7518c84

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

docs/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ What's new
88
- rewrite :py:meth:`Dataset.pint.quantify` and :py:meth:`DataArray.pint.quantify`,
99
to use pint's `parse_units` instead of `parse_expression` (:pull:`40`)
1010
By `Tom Nicholas <https://github.com/TomNicholas>`_.
11+
- ensure the variable which causes the error is explicit if an error occurs in
12+
:py:meth:`Dataset.pint.quantify` (:pull:`43`)
13+
By `Tom Nicholas <https://github.com/TomNicholas>`_.
1114
- refactor the internal conversion functions (:pull:`56`)
1215
By `Justus Magin <https://github.com/keewis>`_.
1316
- allow converting indexes (except :py:class:`pandas.MultiIndex`) (:pull:`56`)

pint_xarray/accessors.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,14 +1145,18 @@ def quantify(self, units=None, unit_registry=None, **unit_kwargs):
11451145

11461146
unit_attrs = conversion.extract_unit_attributes(self.ds)
11471147

1148-
units = {
1149-
name: _decide_units(unit, registry, attr)
1150-
for name, (unit, attr) in zip_mappings(units, unit_attrs).items()
1151-
if unit is not None or attr is not None
1152-
}
1153-
1148+
possible_new_units = zip_mappings(units, unit_attrs)
1149+
new_units = {}
1150+
for name, (unit, attr) in possible_new_units.items():
1151+
if unit is not None or attr is not None:
1152+
try:
1153+
new_units[name] = _decide_units(unit, registry, attr)
1154+
except Exception as e:
1155+
raise ValueError(
1156+
f"Failed to assign units to variable {name}"
1157+
) from e
11541158
return self.ds.pipe(conversion.strip_unit_attributes).pipe(
1155-
conversion.attach_units, units
1159+
conversion.attach_units, new_units
11561160
)
11571161

11581162
def dequantify(self, format=None):

pint_xarray/tests/test_accessors.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,12 @@ def test_error_when_already_units(self, example_quantity_ds):
247247

248248
def test_error_on_nonsense_units(self, example_unitless_ds):
249249
ds = example_unitless_ds
250-
with pytest.raises(UndefinedUnitError):
250+
with pytest.raises(ValueError):
251+
ds.pint.quantify(units={"users": "aecjhbav"})
252+
253+
def test_error_indicates_problematic_variable(self, example_unitless_ds):
254+
ds = example_unitless_ds
255+
with pytest.raises(ValueError, match="users"):
251256
ds.pint.quantify(units={"users": "aecjhbav"})
252257

253258

0 commit comments

Comments
 (0)