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

NumPy clip: np.clip() Syntax, Bounds, and Examples

How numpy.clip limits array values to a lower and upper bound, with one-sided and array bounds, in-place out, and the a_min greater than a_max trap.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.clip() limits the values in an array to an interval: anything below the lower bound becomes the lower bound, and anything above the upper bound becomes the upper bound. This guide covers the call signature, the one-sided and array-valued forms, the trap that appears when the lower bound exceeds the upper bound, and how np.clip relates to the array method and the pandas equivalent. Checked against the NumPy and pandas documentation on 2026-10-29.

What it is used for in research

In analysis code, clipping caps values at fixed limits. Typical uses are keeping a computed proportion inside [0, 1], limiting a sensor or instrument reading to its valid range, and bounding extreme values before a plot or a downstream calculation. Capping extremes at chosen limits is the same idea behind winsorizing, which replaces values beyond a cut-off with the cut-off itself; see winsorizing vs. trimming for how that choice affects an analysis. np.clip applies limits you supply; it does not choose them from percentiles.

Clipping changes your data. If clipped values feed a reported result, state the bounds you used and how many values were changed, so a reader can reproduce the analysis and judge the effect. Keep the original array if you may need the unclipped values later (see the out parameter below).

Install or access

np.clip is part of NumPy. The official installation page (numpy.org/install) says the only prerequisite is Python itself, and gives these commands:

  • pip: pip install numpy
  • conda: conda install numpy, ideally inside an environment created with conda create -n my-env and conda activate my-env

Then import it in your script or notebook: import numpy as np. For background on arrays themselves, see arrays in NumPy.

Step-by-step: clip an array

The signature in the current NumPy documentation is numpy.clip(a, a_min=<no value>, a_max=<no value>, out=None, *, min=<no value>, max=<no value>, **kwargs). The examples below are the ones given in the official reference.

  1. Create or load the array. a = np.arange(10) gives array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).
  2. Clip to an interval. np.clip(a, 1, 8) returns array([1, 1, 2, 3, 4, 5, 6, 7, 8, 8]). Values below 1 became 1 and values above 8 became 8.
  3. Clip one side only. a_min and a_max accept None; if one is None, clipping is not performed on that edge. If both are None, the elements stay the same. For example, np.clip(a, 0, None) sets a floor and no ceiling.
  4. Use array-valued bounds. Both bounds are broadcast against a, so each element can have its own limit. np.clip(a, [3, 4, 1, 1, 1, 4, 4, 4, 4, 4], 8) returns array([3, 4, 2, 3, 4, 5, 6, 7, 8, 8]).
  5. Clip in place if you want to. np.clip(a, 3, 6, out=a) returns array([3, 3, 3, 3, 4, 5, 6, 6, 6, 6]) and the original a now holds those values too. out may be the input array, must have the right shape to hold the result, and its type is preserved.
  6. Use the array method if you prefer. a.clip(min, max) is the ndarray method form; its documentation says one of max or min must be given and refers to numpy.clip as the equivalent function.

The min and max keywords (NumPy 2.1.0 and later)

The reference lists min and max as Array API compatible alternatives to a_min and a_max, marked “New in version 2.1.0”. You can pass either the a_min/a_max pair or the min/max pair. If your environment has an older NumPy, use a_min and a_max.

Output and how to read it

The function returns an ndarray with the elements of a, where values less than a_min are replaced with a_min and values greater than a_max are replaced with a_max. Values already inside the interval are untouched, so in the first example 2 through 7 pass through unchanged and only 0 and 9 are altered. Comparing the output to the input element by element, for example counting positions where they differ, tells you how many values were clipped, which is the number to report alongside your bounds.

Troubleshooting

Every value became the same number

The documentation notes that when a_min is greater than a_max, clip returns an array in which all values equal a_max, and that no check is performed to ensure a_min < a_max. Its second example, np.clip(a, 8, 1) on np.arange(10), returns an array of ten 1s. No check is performed on the order of the bounds, so swapped or miscomputed bounds produce a silently constant array. Check that the lower bound is the smaller number before you clip, especially when the bounds are computed from data.

A one-sided limit seems to do nothing

If both a_min and a_max are None, the array comes back unchanged. Make sure at least one bound is a real value.

The original array changed

You passed out=a, which writes the result into the input array. Omit out, or point it at a separate array of the right shape, to keep the original.

Free alternatives

All of these are free, open-source Python tools documented on their own sites. The first three do the same job on different objects.

Tool Call What the documentation says
NumPy function numpy.clip(a, a_min, a_max, out=None) Returns an ndarray with values limited to the interval; None skips an edge; bounds broadcast against a.
NumPy array method ndarray.clip(min, max, out=None) Returns an array whose values are limited to [min, max]; one of max or min must be given.
pandas DataFrame.clip(lower=None, upper=None, *, axis=None, inplace=False) Trims values at the input thresholds and returns the same type as the caller; axis is unused for a Series; a missing (NA) threshold does not clip the value.
NumPy building blocks np.minimum(a_max, np.maximum(a, a_min)) The documentation describes clip as equivalent to this expression but faster.

The pandas documentation shows the difference in practice. For a DataFrame with col_0 = [9, -3, 0, -1, 5] and col_1 = [-2, -7, 6, 8, -5], df.clip(-4, 6) gives col_0 = [6, -3, 0, -1, 5] and col_1 = [-2, -4, 6, 6, -4]. Passing lists, as in df.clip([-2, -1], [4, 5]), applies a separate threshold to each column (col_0 becomes [4, -2, 0, -1, 4], col_1 becomes [-1, -1, 5, 5, -1]).

FAQ

What does np.clip do?

It limits the values in an array to an interval. Values below the lower bound become the lower bound and values above the upper bound become the upper bound; values in between are unchanged.

Can I clip only the upper or only the lower end?

Yes. Pass None for the edge you do not want to clip. If both are None, the array is returned with the same elements.

Does np.clip change the original array?

Not by default; it returns a new array. It writes into the input only when you pass the input as out, as in np.clip(a, 3, 6, out=a).

What happens if the minimum is larger than the maximum?

All values in the result equal the maximum, and NumPy does not check the order of the bounds or raise an error.

Is clipping the same as removing outliers?

No. Clipping keeps every observation and replaces extreme values with the bound; removing or trimming drops them. See winsorizing vs. trimming and the resistant measure guide for how each affects summary statistics, and descriptive statistics for how to report them.

How do I clip a pandas DataFrame?

Use DataFrame.clip(lower, upper), which returns the same type as the caller. A missing threshold does not clip the value.

Related guides

Sources

Checked 2026-10-29.

Follow CASRAI

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

Ask CASRAI · free to try

Ask about NumPy clip: np.clip() Syntax, Bounds, and Examples

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.