NumPy trapz Replacement: Use trapezoid

numpy.trapz() is no longer the current NumPy API. Modern NumPy code should use numpy.trapezoid() for trapezoidal-rule integration.

The official NumPy documentation covers numpy.trapezoid(), the NumPy 2.0 release notes, and the NumPy 2.4 release notes. SciPy also provides scipy.integrate.trapezoid(). Related PythonPool guides cover NumPy asarray, NumPy cumsum, and NumPy diff.

The old trapz name was deprecated in NumPy 2.0 and removed in NumPy 2.4. If code now raises an attribute error for np.trapz, replace it with np.trapezoid.

The replacement keeps the same core idea: estimate the integral of sampled values using the composite trapezoidal rule.

The important arguments are y, x, dx, and axis. y holds sampled values. x gives sample positions when spacing is not uniform. dx gives constant spacing when x is omitted.

Use x when sample positions are known. Use dx when samples are evenly spaced and only the spacing size matters.

For multi-dimensional arrays, axis controls the direction along which integration happens.

If a project already depends on SciPy for numerical integration, scipy.integrate.trapezoid() is also a current option.

The examples below use the current NumPy name so they continue to work on newer NumPy releases.

When migrating, avoid changing the numerical method at the same time. First replace the old function name with trapezoid, then run tests and review any expected values separately.

This keeps the change small: one API update, the same sampled data, and the same spacing arguments.

Replace trapz With trapezoid

The direct migration is usually a function-name change.

import numpy as np

y = np.array([0.0, 1.0, 2.0])

area = np.trapezoid(y)

print(area)

This estimates the area using unit spacing between samples.

Old code often used np.trapz(y) for the same task.

Use np.trapezoid(y) in maintained code.

This avoids the removed name on newer NumPy versions.

If a package supports multiple NumPy versions, using trapezoid is still the clearer forward-looking choice because it works with the current public API.

Use x Sample Positions

Pass x when the sample positions are known.

import numpy as np

x = np.array([0.0, 0.5, 2.0])
y = np.array([0.0, 1.0, 1.5])

area = np.trapezoid(y, x=x)

print(area)

This uses the actual spacing between sample positions.

Use this form when samples are not evenly spaced.

The positions in x are paired with the values in y.

Check that both arrays line up before integrating.

The length of x should match the length of y along the integration axis. A mismatch usually means the sample positions and sampled values came from different steps in the pipeline.

Use dx For Even Spacing

Pass dx when samples are evenly spaced.

import numpy as np

y = np.array([1.0, 2.0, 4.0, 8.0])

area = np.trapezoid(y, dx=0.25)

print(area)

This treats each gap between samples as 0.25.

Use dx instead of building an x array when the spacing is constant.

If spacing changes between samples, use x instead.

Choosing the wrong spacing changes the estimated area.

For unit-aware data, keep the units near the calculation. A dx value in seconds gives a different interpretation than a value in minutes or meters.

Integrate Along An Axis

Use axis for multi-dimensional arrays.

import numpy as np

y = np.array([
    [0.0, 1.0, 2.0],
    [2.0, 3.0, 4.0],
])

area = np.trapezoid(y, dx=1.0, axis=1)

print(area)

This integrates across each row.

The selected axis becomes shorter through the integration result.

Use axis=0 to integrate down columns instead.

Print the input shape and output shape when reviewing axis behavior.

Axis mistakes are easy to miss because the code still returns numbers. Compare the result shape with the dimension you intended to integrate over.

Use SciPy When Appropriate

SciPy has a matching trapezoid integration function.

import numpy as np
from scipy import integrate

x = np.array([0.0, 1.0, 2.0])
y = np.array([0.0, 1.0, 0.0])

area = integrate.trapezoid(y, x=x)

print(area)

This is useful in projects that already rely on SciPy integration tools.

For simple NumPy-only workflows, np.trapezoid() is usually enough.

Choose one option and keep it consistent across the project.

Avoid mixing old and new names in the same codebase.

If SciPy is already installed for integration, using its function can be consistent with other numerical routines. If NumPy is the only dependency needed, adding SciPy only for this replacement is usually unnecessary.

Update Old Code Safely

When migrating, change the function name and keep the arguments the same unless tests show a separate issue.

import numpy as np

samples = np.array([0.0, 2.0, 2.0, 0.0])

area = np.trapezoid(samples, dx=0.5)

print(area)

After replacing the old name, run tests around any code that depends on the result.

Pay special attention to units, sample spacing, and axis choices.

Also search the codebase for both trapz and trapezoid. A partial migration can leave old calls hidden in tests, notebooks, or utility modules.

In short, replace np.trapz(...) with np.trapezoid(...), use x for explicit sample positions, use dx for even spacing, and use SciPy’s integrate.trapezoid() when the project already uses SciPy integration tools.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Anne Ginzburg
Anne Ginzburg
4 years ago

What if you have a function, such as sin(x) that you would like to integrate? Thank you so much, I appreciate this tutorial!

Pratik Kinage
Admin
3 years ago
Reply to  Anne Ginzburg

np.trapz(np.sin(x), x) This would help.