Skip to content

Commit

Permalink
MNT: Handle trapz for numpy>=2
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 committed Apr 26, 2024
1 parent 03fc7e1 commit 6e707ec
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions pint/facets/numpy/numpy_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,21 +742,23 @@ def _base_unit_if_needed(a):


@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 6e707ec

Please sign in to comment.