Written and maintained by CASRAI Editorial Board
Last updated
np.interp is NumPy’s one-dimensional linear interpolation function: given known points (xp, fp), it returns the piecewise linear value at any new x. It fills a value between two measurements, resamples a series onto a new grid, or reads a value off a calibration curve, and it needs nothing beyond NumPy. Searchers who type “numpy interpolate” almost always want this function; this guide covers it step by step, then shows when to move to SciPy.
What np.interp is used for in research
Research data rarely arrives on the grid an analysis needs. np.interp handles the common cases where straight-line interpolation between neighbouring points is acceptable:
- Reading a value from a calibration or standard curve at a measured x.
- Resampling a time series or spectrum onto a common set of x values so two datasets can be compared point by point.
- Filling a gap between two known observations when a linear assumption is defensible and documented.
- Interpolating angular data (for example directions in degrees) with a periodic wrap-around.
It is not a statistical model and gives no uncertainty estimate. The interpolated values are straight-line readings between your data points, so report them as such. Interpolating inside a distribution to find a quantile is a different use of the word; see percentiles and quantiles for that.
Install or access NumPy
NumPy’s install page (numpy.org/install, checked 2026-10-29) lists these routes:
pip install numpyin your Python environment.conda install numpyinside a conda environment.- A virtual environment: create it with
python -m venv my-env, activate it withsource my-env/bin/activate(macOS and Linux) ormy-env\Scripts\activate(Windows), then runpip install numpy.
For a project others must be able to rerun, pin the environment; see sharing a reproducible conda environment and the dictionary entry on computational reproducibility.
Step-by-step: interpolate with np.interp
The signature in the current NumPy reference is numpy.interp(x, xp, fp, left=None, right=None, period=None).
- Import NumPy.
import numpy as np - Define the known points.
xpholds the x coordinates (one-dimensional, increasing) andfpholds the matching values, with the same length.xp = [1, 2, 3] fp = [3, 2, 0] - Check that xp is increasing. NumPy does not enforce it (see the next section).
np.all(np.diff(xp) > 0)must beTrue. - Call np.interp at one or more x values.
np.interp(2.5, xp, fp) # 1.0 np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp) # array([3. , 3. , 2.5 , 0.56, 0. ]) - Decide what happens outside the data. By default,
xbelowxp[0]returnsfp[0]andxabovexp[-1]returnsfp[-1]. That is why 0 and 3.14 above return 3.0 and 0.0. To flag out-of-range inputs instead, setleftandright:UNDEF = -99.0 np.interp(3.14, xp, fp, right=UNDEF) # -99.0
Why xp must be increasing
If period is not given, the NumPy reference says xp must be increasing, and it also says this is not enforced: a non-increasing xp returns results that are meaningless. There is no error to warn you. For example, np.interp(1.5, [1, 3, 2], [10, 30, 20]) runs without complaint and returns 15.0, even though the inputs are not a valid curve. Some tutorials state that xp is sorted for you; the official reference says the opposite for the non-periodic case.
Guard against this in any pipeline:
assert np.all(np.diff(xp) > 0), "xp must be strictly increasing"
If your data is unsorted, sort xp and reorder fp with the same index (order = np.argsort(xp)). The reference also states that xp cannot contain NaN. It does not document how NaN values in x or fp are handled, so remove or resolve them before the call rather than relying on a behaviour you have not tested; for principled handling of gaps in a dataset see multiple imputation: a practical protocol.
Output and how to read it
- The result is a float (or complex) when
xis a scalar, otherwise an array with the same shape asx. - Inside the range of
xp, each value lies on the straight line between its two neighbouring known points. At 2.5 between (2, 2) and (3, 0) the value is 1.0. - Values at or beyond the ends come back as the end value of
fp, or as yourleft/rightsentinel. A flat run at the edges of an output array is clamping, not data.
Periodic and angular data
For data on a circle, pass period. The reference states that left and right are ignored when period is set. The documented example uses degrees:
x = [-180, -170, -185, 185, -10, -5, 0, 365]
xp = [190, -190, 350, -350]
fp = [5, 10, 3, 4]
np.interp(x, xp, fp, period=360)
# array([7.5 , 5. , 8.75, 6.25, 3. , 3.25, 3.5 , 3.75])
Complex values
fp may be complex: np.interp([1.5, 4.0], [2, 3, 5], [1.0j, 0, 2+3j]) returns array([0.+1.j, 1.+1.5j]).
Numpy interpolate: when np.interp is not enough
The SciPy interpolation tutorial states that if all you need is linear (broken line) interpolation, numpy.interp is the routine to use, and that one limitation is that it does not allow controlling extrapolation. When your analysis needs a smooth curve, a different edge behaviour, or shape preservation, move to scipy.interpolate:
| Need | Use | What the SciPy documentation says |
|---|---|---|
| Straight lines between points, clamped ends | numpy.interp |
The routine for linear interpolation; does not allow controlling extrapolation. |
| Linear interpolation with linear extrapolation | make_interp_spline(x, y, k=1) |
k=1 gives linear interpolation, and splines extrapolate by default. |
| A smooth curve through the points | CubicSpline |
Listed in the 1-D interpolation tutorial as the smoother option. |
| Preserve the local shape of the data | PchipInterpolator, Akima1DInterpolator |
These attempt to preserve the local shape implied by the data. |
Existing code using interp1d |
Migrate to the options above | The class is legacy and will no longer receive updates; new code should use the modern alternatives. |
Higher-order curves can overshoot between points, and extrapolating beyond the measured range is a modelling assumption, not a measurement. State the method and the range of x in the methods section of anything you publish; for related handling of extreme values see winsorizing versus trimming.
Troubleshooting
- Odd, wrong-looking values with no error. Check that
xpis increasing withnp.all(np.diff(xp) > 0). - ValueError. The reference lists three causes:
xpandfphave different lengths, they are not one-dimensional, orperiodequals 0. - A flat tail in the output. Your
xvalues are outsidexp; the end values are being returned. Useleft/rightto mark them, or switch to a SciPy method if you need extrapolation. - Angles that do not wrap. Pass
period=360(or the period of your data).
Free alternatives
All of the options below are open-source Python libraries distributed with the standard scientific stack.
| Library function | Part of | Typical use |
|---|---|---|
numpy.interp |
NumPy | 1-D linear interpolation, no extra dependency |
scipy.interpolate.make_interp_spline |
SciPy | Spline of chosen degree k, including k=1 |
scipy.interpolate.CubicSpline |
SciPy | Smooth cubic curve |
scipy.interpolate.PchipInterpolator / Akima1DInterpolator |
SciPy | Local shape preservation |
FAQ
How do I interpolate in NumPy?
Use np.interp(x, xp, fp) with an increasing xp array of known x values and a same-length fp array of known values. It returns the linearly interpolated value at each x.
Does np.interp extrapolate?
No. Values outside the range of xp are clamped to fp[0] and fp[-1] by default, or set to left and right if given. SciPy states that numpy.interp does not allow controlling the extrapolation.
Does xp have to be sorted?
Yes, increasing, unless you use period. NumPy does not check it, so check it yourself.
Is interp1d still the way to interpolate in SciPy?
SciPy documents interp1d as legacy that will no longer receive updates, and recommends modern alternatives for new code.
Sources (official documentation, checked 2026-10-29)
- NumPy reference: numpy.interp
- NumPy: Installing NumPy
- SciPy: 1-D interpolation tutorial
- SciPy: scipy.interpolate.interp1d (legacy)








