Skip to content

Commit 4822bd8

Browse files
authored
fixes for the pint tests (#8983)
* use the `Quantity` constructor instead of multiplying * manually install `pint` deps into the upstream-dev CI * stop explicitly installing `pint` dependencies
1 parent 94c8432 commit 4822bd8

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

ci/install-upstream-wheels.sh

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ python -m pip install \
4848
--pre \
4949
--upgrade \
5050
pyarrow
51+
# manually install `pint` to pull in new dependencies
52+
python -m pip install --upgrade pint
5153
python -m pip install \
5254
--no-deps \
5355
--upgrade \

xarray/tests/test_units.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
# always be treated like ndarrays
3535
unit_registry = pint.UnitRegistry(force_ndarray_like=True)
3636
Quantity = unit_registry.Quantity
37+
no_unit_values = ("none", None)
3738

3839

3940
pytestmark = [
@@ -91,17 +92,13 @@ def array_strip_units(array):
9192

9293

9394
def array_attach_units(data, unit):
94-
if isinstance(data, Quantity):
95+
if isinstance(data, Quantity) and data.units != unit:
9596
raise ValueError(f"cannot attach unit {unit} to quantity {data}")
9697

97-
try:
98-
quantity = data * unit
99-
except np.core._exceptions.UFuncTypeError:
100-
if isinstance(unit, unit_registry.Unit):
101-
raise
102-
103-
quantity = data
98+
if unit in no_unit_values or (isinstance(unit, int) and unit == 1):
99+
return data
104100

101+
quantity = unit_registry.Quantity(data, unit)
105102
return quantity
106103

107104

0 commit comments

Comments
 (0)