From d331aafbc12b464baaf44745fd8e05ab756aa9e1 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 24 Dec 2023 05:34:44 +0000 Subject: [PATCH 1/3] check magnitude is array --- pint/facets/numpy/quantity.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pint/facets/numpy/quantity.py b/pint/facets/numpy/quantity.py index 5257766bc..561c1d31c 100644 --- a/pint/facets/numpy/quantity.py +++ b/pint/facets/numpy/quantity.py @@ -115,11 +115,12 @@ def _numpy_method_wrap(self, func, *args, **kwargs): return value def __array__(self, t=None) -> np.ndarray: - warnings.warn( - "The unit of the quantity is stripped when downcasting to ndarray.", - UnitStrippedWarning, - stacklevel=2, - ) + if isinstance(self._magnitude, np.ndarray): + warnings.warn( + "The unit of the quantity is stripped when downcasting to ndarray.", + UnitStrippedWarning, + stacklevel=2, + ) return _to_magnitude(self._magnitude, force_ndarray=True) def clip(self, min=None, max=None, out=None, **kwargs): From 526fc056a860fdcd8c61dd5dc437ba3dd163336f Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 26 Dec 2023 02:25:04 +0000 Subject: [PATCH 2/3] numpy check --- pint/facets/numpy/quantity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pint/facets/numpy/quantity.py b/pint/facets/numpy/quantity.py index 561c1d31c..5b8d268a9 100644 --- a/pint/facets/numpy/quantity.py +++ b/pint/facets/numpy/quantity.py @@ -16,7 +16,7 @@ from ..plain import PlainQuantity, MagnitudeT from ..._typing import Shape -from ...compat import _to_magnitude, np +from ...compat import _to_magnitude, np, HAS_NUMPY from ...errors import DimensionalityError, PintTypeError, UnitStrippedWarning from .numpy_func import ( HANDLED_UFUNCS, @@ -115,7 +115,7 @@ def _numpy_method_wrap(self, func, *args, **kwargs): return value def __array__(self, t=None) -> np.ndarray: - if isinstance(self._magnitude, np.ndarray): + if HAS_NUMPY and isinstance(self._magnitude, np.ndarray): warnings.warn( "The unit of the quantity is stripped when downcasting to ndarray.", UnitStrippedWarning, From 188c87ea44d7ca106bd35a2673c05682bb5ec254 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 1 Jan 2024 00:26:20 +0000 Subject: [PATCH 3/3] allow uncertainties in to_compact --- pint/facets/plain/qto.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pint/facets/plain/qto.py b/pint/facets/plain/qto.py index 9cd8a780a..726523763 100644 --- a/pint/facets/plain/qto.py +++ b/pint/facets/plain/qto.py @@ -100,7 +100,9 @@ def to_compact( """ - if not isinstance(quantity.magnitude, numbers.Number): + if not isinstance(quantity.magnitude, numbers.Number) and not hasattr( + quantity.magnitude, "nominal_value" + ): msg = "to_compact applied to non numerical types " "has an undefined behavior." w = RuntimeWarning(msg) warnings.warn(w, stacklevel=2) @@ -137,6 +139,9 @@ def to_compact( q_base = quantity.to(unit) magnitude = q_base.magnitude + # Support uncertainties + if hasattr(magnitude, "nominal_value"): + magnitude = magnitude.nominal_value units = list(q_base._units.items()) units_numerator = [a for a in units if a[1] > 0]