interp1d() takes arrays of known x and y data points and returns a new function you can call with any x value within the original data's range, returning an estimated y value based on the surrounding known points — 'linear', the default, connects consecutive points with straight lines, while kind='cubic', or other options, fits smoother curves through the points instead. By default, calling the resulting function with an x value outside the original data's range raises an error, since extrapolating beyond known data is a fundamentally different, riskier operation than interpolating within it.
1Understanding interpolate.interp1d()
interp1d() takes arrays of known x and y data points and returns a new function you can call with any x value within the original data's range, returning an estimated y value based on the surrounding known points — 'linear', the default, connects consecutive points with straight lines, while kind='cubic', or other options, fits smoother curves through the points instead. By default, calling the resulting function with an x value outside the original data's range raises an error, since extrapolating beyond known data is a fundamentally different, riskier operation than interpolating within it.
By default, interp1d() raises an error for any x value outside the original data's range, rather than silently extrapolating — pass fill_value and bounds_error=False explicitly if you specifically want it to extrapolate, or return a placeholder, instead of erroring on out-of-range values.
from scipy.interpolate import interp1d
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))2Practical Example
Here is a real-world application of interpolate.interp1d() showing how it is used in production SciPy code.
from scipy.interpolate import interp1d
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([0, 1, 4, 9])
f = interp1d(x, y, kind="cubic")
print(round(float(f(1.5)), 2))3Best Practices
Follow these guidelines when working with interpolate.interp1d():
1. Use kind='cubic', or another smooth option, instead of the default linear interpolation when you need a smoother estimated curve through the data, not just straight-line segments
2. Leave the default error-on-extrapolation behavior in place unless you specifically intend to extrapolate, since estimating far beyond known data is much less reliable than interpolating within it
3. Pass fill_value and bounds_error=False explicitly when you do need extrapolation or a specific placeholder value for out-of-range inputs, rather than letting the default error occur unexpectedly
Tip: By default, interp1d() raises an error for any x value outside the original data's range, rather than silently extrapolating — pass fill_value and bounds_error=False explicitly if you specifically want it to extrapolate, or return a placeholder, instead of erroring on out-of-range values.
from scipy.interpolate import interp1d
import numpy as np
x = np.array([0, 1, 2, 3])
y = np.array([0, 10, 20, 30])
f = interp1d(x, y)
print(f(1.5))