diff --git a/ci/install-upstream-wheels.sh b/ci/install-upstream-wheels.sh index b43c6a61ec1..d728768439a 100755 --- a/ci/install-upstream-wheels.sh +++ b/ci/install-upstream-wheels.sh @@ -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 \ diff --git a/xarray/tests/test_units.py b/xarray/tests/test_units.py index 2f11fe688b7..0e8fbe9198b 100644 --- a/xarray/tests/test_units.py +++ b/xarray/tests/test_units.py @@ -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 = [ @@ -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