Skip to content

Commit

Permalink
MNT: Handle trapz for numpy>=2 (#1971)
Browse files Browse the repository at this point in the history
trapz has been deprecated in favor of the newly available trapezoid
function. This wraps the new function and avoids a DeprecationWarning on
numpy>=2.
  • Loading branch information
dopplershift authored May 12, 2024
1 parent 2b4a8b7 commit feaa945
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pint/facets/numpy/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,22 +741,25 @@ def _base_unit_if_needed(a):
raise OffsetUnitCalculusError(a.units)


# Can remove trapz wrapping when we only support numpy>=2
@implements("trapz", "function")
@implements("trapezoid", "function")
def _trapz(y, x=None, dx=1.0, **kwargs):
trapezoid = np.trapezoid if hasattr(np, "trapezoid") else np.trapz
y = _base_unit_if_needed(y)
units = y.units
if x is not None:
if hasattr(x, "units"):
x = _base_unit_if_needed(x)
units *= x.units
x = x._magnitude
ret = np.trapz(y._magnitude, x, **kwargs)
ret = trapezoid(y._magnitude, x, **kwargs)
else:
if hasattr(dx, "units"):
dx = _base_unit_if_needed(dx)
units *= dx.units
dx = dx._magnitude
ret = np.trapz(y._magnitude, dx=dx, **kwargs)
ret = trapezoid(y._magnitude, dx=dx, **kwargs)

return y.units._REGISTRY.Quantity(ret, units)

Expand Down

0 comments on commit feaa945

Please sign in to comment.