-
Notifications
You must be signed in to change notification settings - Fork 499
Open
Description
to_compact() is a no-op on NumpyQuantitys and results in the warning:
UndefinedBehavior: to_compact applied to non numerical types has an undefined behavior.
qty.to_compact()
The problem appears, because for numpy quantities, the qty.magnitude is of type ndarray, which is non-numeric, an therefore qto.to_compact() returns the quantity without change:
Lines 95 to 103 in 40936b9
| if not isinstance(quantity.magnitude, numbers.Number) and not hasattr( | |
| quantity.magnitude, "nominal_value" | |
| ): | |
| warnings.warn( | |
| "to_compact applied to non numerical types has an undefined behavior.", | |
| UndefinedBehavior, | |
| stacklevel=2, | |
| ) | |
| return quantity |
The method should be modified to accept ndarray by using either the maximum or minimum order of magnitude in the array to select the unit prefix.
Minimal example to reproduce the problem:
from pint import UnitRegistry
ureg = UnitRegistry(force_ndarray=True)
qty = ureg[Any]('1e+3 g')
qty.to_compact() # 1000.0 gramReactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels