Skip to main content
v2026.11,858 entries · CC-BY 4.0

Interpolation in NumPy: np.interp Step by Step

How to do interpolation in NumPy with np.interp, why xp must be increasing, how extrapolation behaves, and when SciPy methods are the better choice.

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 numpy in your Python environment.
  • conda install numpy inside a conda environment.
  • A virtual environment: create it with python -m venv my-env, activate it with source my-env/bin/activate (macOS and Linux) or my-env\Scripts\activate (Windows), then run pip 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).

  1. Import NumPy. import numpy as np
  2. Define the known points. xp holds the x coordinates (one-dimensional, increasing) and fp holds the matching values, with the same length.
    xp = [1, 2, 3]
    fp = [3, 2, 0]
  3. Check that xp is increasing. NumPy does not enforce it (see the next section). np.all(np.diff(xp) > 0) must be True.
  4. 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.  ])
  5. Decide what happens outside the data. By default, x below xp[0] returns fp[0] and x above xp[-1] returns fp[-1]. That is why 0 and 3.14 above return 3.0 and 0.0. To flag out-of-range inputs instead, set left and right:
    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 x is a scalar, otherwise an array with the same shape as x.
  • 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 your left/right sentinel. 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 xp is increasing with np.all(np.diff(xp) > 0).
  • ValueError. The reference lists three causes: xp and fp have different lengths, they are not one-dimensional, or period equals 0.
  • A flat tail in the output. Your x values are outside xp; the end values are being returned. Use left/right to 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)

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Interpolation in NumPy: np.interp Step by Step

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.