Skip to content

Commit

Permalink
fixes for the pint tests (#8983)
Browse files Browse the repository at this point in the history
* use the `Quantity` constructor instead of multiplying

* manually install `pint` deps into the upstream-dev CI

* stop explicitly installing `pint` dependencies
  • Loading branch information
keewis authored and andersy005 committed Jun 14, 2024
1 parent 7009b2a commit fb5c6a7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
2 changes: 2 additions & 0 deletions ci/install-upstream-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ python -m pip install \
--pre \
--upgrade \
pyarrow
# manually install `pint` to pull in new dependencies
python -m pip install --upgrade pint
python -m pip install \
--no-deps \
--upgrade \
Expand Down
13 changes: 5 additions & 8 deletions xarray/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# always be treated like ndarrays
unit_registry = pint.UnitRegistry(force_ndarray_like=True)
Quantity = unit_registry.Quantity
no_unit_values = ("none", None)


pytestmark = [
Expand Down Expand Up @@ -91,17 +92,13 @@ def array_strip_units(array):


def array_attach_units(data, unit):
if isinstance(data, Quantity):
if isinstance(data, Quantity) and data.units != unit:
raise ValueError(f"cannot attach unit {unit} to quantity {data}")

try:
quantity = data * unit
except np.core._exceptions.UFuncTypeError:
if isinstance(unit, unit_registry.Unit):
raise

quantity = data
if unit in no_unit_values or (isinstance(unit, int) and unit == 1):
return data

quantity = unit_registry.Quantity(data, unit)
return quantity


Expand Down

0 comments on commit fb5c6a7

Please sign in to comment.