Skip to content

[Compatibility Issue] np.trapz removed in NumPy 2.4.0 — use np.trapezoid instead #28940

Description

@xyf5432

Affected Files

  • ivy/functional/backends/numpy/elementwise.py:749
  • ivy/functional/backends/numpy/experimental/statistical.py:567

Current Code

# ivy/functional/backends/numpy/elementwise.py:749
def trapz(y, /, *, x=None, dx=1.0, axis=-1, out=None):
    return np.trapz(y, x=x, dx=dx, axis=axis)

# ivy/functional/backends/numpy/experimental/statistical.py:567
integral = np.trapz(y, t)

Root Cause

numpy.trapz was deprecated in NumPy 2.0 (2023-08-18) and fully removed in NumPy 2.4.0 (2025-12-20). In NumPy >= 2.4.0, any call to np.trapz() raises:

AttributeError: module 'numpy' has no attribute 'trapz'

Ivy defines its own trapz API across multiple backends (torch, jax, tensorflow, numpy). The numpy backend directly delegates to np.trapz, which will break.

Impact

  • Severity: High — hard crash at runtime on NumPy >= 2.4.0
  • The numpy backend trapz() is called via ivy.trapz() → backend dispatch → np.trapz()
  • Also affects the numpy frontend: ivy.functional.frontends.numpy.trapz()ivy.trapz() → numpy backend → np.trapz()
  • The experimental igamma_cal function in statistical.py is also affected

Solution

# ivy/functional/backends/numpy/elementwise.py:749
# Before:
return np.trapz(y, x=x, dx=dx, axis=axis)
# After:
return np.trapezoid(y, x=x, dx=dx, axis=axis)

# ivy/functional/backends/numpy/experimental/statistical.py:567
# Before:
integral = np.trapz(y, t)
# After:
integral = np.trapezoid(y, t)

trapezoid has the same signature: trapezoid(y, x=None, dx=1.0, axis=-1).

For NumPy < 2.0 backward compatibility:

try:
    from numpy import trapezoid
except ImportError:
    from numpy import trapz as trapezoid

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions