Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Class

interp1d

Creates a callable interpolator for one-dimensional sampled data. It supports linear, nearest-neighbor, spline-based, and directional interpolation with configurable axis and out-of-range behavior.

How it works

Create an interpolator from one-dimensional coordinates and corresponding values, then call the returned object with new coordinates. The selected kind controls how values between samples are estimated, while axis selects the dimension of multidimensional y that corresponds to x.

Syntax

scipy.interpolate.interp1d(x, y, kind='linear', axis=-1, copy=True, bounds_error=None, fill_value=nan, assume_sorted=False)

Parameters

NameTypeDescription
xarray_likeOne-dimensional array of real sample coordinates.
yarray_likeN-dimensional real values; its length along the interpolation axis must match x.
kindstr or int, optionalInterpolation method name or spline order; default is linear.
axisint, optionalAxis of y corresponding to x; default is -1.
copybool, optionalWhether to copy input arrays; default is True.
bounds_errorbool, optionalControls whether out-of-range evaluation raises ValueError.
fill_valuearray-like or two-element tuple, optionalValue assigned outside the x range when bounds_error is False; default is nan.
assume_sortedbool, optionalIf True, x is assumed sorted and is not sorted internally; default is False.

Returns

A callable interpolator that evaluates estimated values at new points.

Key points

  • interp1d creates a callable approximation of a one-dimensional function from sampled data.
  • The constructed object is evaluated like a function at new coordinates.
  • kind selects the interpolation rule, including linear, nearest, spline-based, previous, and next variants.
  • For multidimensional y, interpolation uses the final axis unless axis is explicitly changed.
  • x must provide a one-dimensional sequence of real sample coordinates.
  • The documented string kinds include linear, nearest, nearest-up, zero, slinear, quadratic, cubic, previous, and next.

Common mistakes

  • Ensure that y has exactly as many samples along the interpolation axis as x has coordinates.
  • Avoid NaNs in the input arrays because the result is not defined.
  • With bounds_error=True, evaluating outside x raises ValueError; configure fill_value instead when out-of-range results are needed.

Example: Linear interpolation

import numpy as np
from scipy.interpolate import interp1d

x = np.array([0.0, 1.0, 2.0])
y = np.array([0.0, 1.0, 4.0])
f = interp1d(x, y, kind="linear")
print(f([0.5, 1.5]))  # [0.5 2.5]

Creates a linear interpolator from known points and evaluates it at two intermediate coordinates.

When to use it

Resampling sampled data

Estimate values between measured samples on a one-dimensional coordinate grid.

Interpolating multidimensional arrays

Evaluate vector- or tensor-valued samples along one selected dimension.

Custom interpolation behavior

Choose nearest, spline, previous, or next behavior for application-specific lookup rules.

Interview questions

QWhat does interp1d return and how is it used?

interp1d builds a callable interpolator from x and y samples. Calling that object estimates values at new x coordinates using the requested interpolation kind.

Follow-up: How would you handle multidimensional y? Set axis to the dimension containing the sample sequence, whose length must equal x.

QWhich interpolation methods can be selected?

The default kind is linear. Other choices include nearest, zero, slinear, quadratic, cubic, previous, and next; an integer can specify spline order.

Follow-up: Which parameter controls the sampled dimension? axis does, and its default is the last axis of y.

QHow does interp1d handle values outside the data range?

bounds_error determines behavior outside the x range: True raises ValueError, while False assigns fill_value. The default bounds_error=None is resolved together with fill_value according to the documented boundary behavior.

Follow-up: How can endpoint values be used for out-of-range inputs? Supply a two-element fill_value for below- and above-range values.

  • scipy.interpolate.CubicSpline
  • scipy.interpolate.PchipInterpolator
  • scipy.interpolate.Akima1DInterpolator

Source: scipy.interpolate.interp1d. Checked 2026-10-15.