Skip to content

Commit

Permalink
MNT: Handle trapz for numpy>=2 (hgrecco#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 and takluyver committed Jun 18, 2024
1 parent 31a9748 commit 61f6bf7
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):

# NP2 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 61f6bf7

Please sign in to comment.