Skip to content

trapz incorrectly handling temperature #1593

@dopplershift

Description

@dopplershift

The following example ends up raising

import pint

ureg = pint.UnitRegistry(autoconvert_offset_to_baseunit=True)

t = np.array([1., 5., 10.]) * ureg.degC
z = np.array([0., 2., 4.]) * ureg.m
np.trapz(t, x=z) / (z[-1] - z[0])

with

---------------------------------------------------------------------------
OffsetUnitCalculusError                   Traceback (most recent call last)
Cell In [36], line 9
      5 t = np.array([1., 5., 10.]) * ureg.degC
      7 z = np.array([0., 2., 4.]) * ureg.m
----> 9 np.trapz(t, x=z) / (z[-1] - z[0])

File ~/miniconda3/envs/py310/lib/python3.10/site-packages/pint/quantity.py:1341, in Quantity.__truediv__(self, other)
   1340 def __truediv__(self, other):
-> 1341     return self._mul_div(other, operator.truediv)

File ~/miniconda3/envs/py310/lib/python3.10/site-packages/pint/quantity.py:139, in check_implemented.<locals>.wrapped(self, *args, **kwargs)
    137 elif isinstance(other, list) and other and isinstance(other[0], type(self)):
    138     return NotImplemented
--> 139 return f(self, *args, **kwargs)

File ~/miniconda3/envs/py310/lib/python3.10/site-packages/pint/quantity.py:119, in ireduce_dimensions.<locals>.wrapped(self, *args, **kwargs)
    118 def wrapped(self, *args, **kwargs):
--> 119     result = f(self, *args, **kwargs)
    120     try:
    121         if result._REGISTRY.auto_reduce_dimensions:

File ~/miniconda3/envs/py310/lib/python3.10/site-packages/pint/quantity.py:1302, in Quantity._mul_div(self, other, magnitude_op, units_op)
   1299 new_self = self
   1301 if not self._ok_for_muldiv(no_offset_units_self):
-> 1302     raise OffsetUnitCalculusError(self._units, other._units)
   1303 elif no_offset_units_self == 1 and len(self._units) == 1:
   1304     new_self = self.to_root_units()

OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius * meter, meter). See https://pint.readthedocs.io/en/latest/nonmult.html for guidance.

This should not be yielding a value with "degree_Celsius * meter" (that's incorrect). Without the autoconvert flag set to True, it should probably error, and with it True, the values should be converted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    numpyNumpy related bug/enhancement

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions