Written and maintained by CASRAI Editorial Board
Last updated
numpy.floor (usually written np.floor) rounds every element of an array down to the nearest integer that is less than or equal to it. The result keeps a floating-point type for float input, and negative numbers move away from zero: np.floor(-1.5) is -2.0, not -1.0. The sections below show the call, the output, how it differs from its neighbours (ceil, trunc, rint, math.floor), and how researchers use it to bin continuous measurements.
What it is used for in research
The NumPy manual defines the floor of a scalar x as the largest integer i such that i <= x. In analysis code that definition is most often used to turn a continuous measurement into a bin label or to truncate a quantity to whole units while keeping the rule predictable for negative values.
- Binning continuous values.
np.floor(x / width) * widthreturns the lower edge of the bin of widthwidththat contains each value, andnp.floor(x / width)returns the bin index (see the worked example below). - Whole-unit truncation. Reducing a fractional measurement such as a duration or a length to whole units, rounding down rather than to the nearest value.
- Consistent handling of negatives. Because floor always moves toward negative infinity, a pipeline gives the same answer on either side of zero. For other element-wise transforms of the same array, see NumPy log.
Install or access
The NumPy project lists these official install routes (numpy.org/install, checked 2026-10-29):
- pip:
pip install numpy - conda:
conda create -n my-env, thenconda activate my-env, thenconda install numpy - virtual environment:
python -m venv my-env, thensource my-env/bin/activate(macOS/Linux) ormy-env\Scripts\activate(Windows), thenpip install numpy
The examples on this page were run with Python 3.14.6 and NumPy 2.5.1. np.floor ships with NumPy; no extra package is needed.
Step-by-step: round down an array
The signature in the NumPy 2.5 manual is numpy.floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]). Only x (array_like, the input data) is required.
- Import NumPy.
import numpy as np - Build the array.
a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) - Call the function.
np.floor(a) - Read the result.
array([-2., -2., -1., 0., 1., 1., 2.]) - Optional: convert to integers.
np.floor(a).astype(int)returnsarray([-2, -2, -1, 0, 1, 1, 2]).
A scalar input returns a scalar: np.floor(-2.5) returns -3.0 (a numpy.float64).
Writing into an existing array with out and where
Per the manual, out is a location into which the result is stored; if omitted, a freshly allocated array is returned. where is a condition broadcast over the input: where it is True the output is set to the ufunc result, elsewhere the output keeps its original value. Run on the array a above:
out = np.full_like(a, -99.0)
np.floor(a, out=out, where=a > 0)
print(out) # [-99. -99. -99. 0. 1. 1. 2.]
The three positions where the condition was False keep the placeholder value -99., as the manual describes.
Worked example: lower bin edges for a measurement
x = np.array([3.2, 7.9, 12.4, 15.0, 19.99, 4.5])
w = 5
np.floor(x / w) * w # array([ 0., 5., 10., 15., 15., 0.])
np.floor(x / w).astype(int) # array([0, 1, 2, 3, 3, 0])
The first line gives each value’s lower bin edge (width 5); the second gives the bin index. A value exactly on an edge, such as 15.0, falls in the bin that starts at that edge. For a clamped range rather than bins, see NumPy clip.
Output and how to read it
- Negatives round toward minus infinity. -1.7 becomes -2.0 and -0.2 becomes -1.0. The manual notes that some spreadsheet programs compute a floor-towards-zero (where floor(-2.5) would be -2); NumPy instead uses the definition where floor(-2.5) is -3. The floor-towards-zero operation is called
fixin NumPy. - Float in, float out. The example output shows
-2.with a decimal point: the dtype stays float64. Use.astype(int)when you need integer labels, for example as array indices. - Integer arrays. In the NumPy 2.5.1 used here,
np.floor(np.array([1, 2, 3]))returned anint64array[1 2 3]. - Negative zero. The sibling functions
ceil,truncandrintprinted-0.for inputs between -1 and 0 in the comparison below; this is a display of the sign of zero and compares equal to0..
Troubleshooting
- Result is float and fails as an index. Convert with
.astype(int)after flooring. - Wrong answer on negative data expecting truncation. If you expected -1.7 to become -1, you want truncation toward zero (
np.trunc), not floor. - NaN and infinities. They pass through:
np.floor(np.array([1.5, np.nan, np.inf, -np.inf]))returned[1. nan inf -inf]. Remove or mask NaN values first if bin indices must be integers, since NaN cannot be cast to an integer. - TypeError on text.
np.floor(np.array(['a']))raisedTypeError: ufunc 'floor' not supported for the input types. Convert strings to numbers first; see NumPy loadtxt for reading numeric files. - Bin index looks off by one. A value exactly on an edge falls in the bin that starts there (15.0 with width 5 gives index 3); if you need the opposite convention, use
np.ceilon the same quotient and subtract one.
Free alternatives and neighbours
Run on a = [-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0] with NumPy 2.5.1 (the floor row matches the NumPy manual’s own example):
| Function | Rule | Output on a |
|---|---|---|
np.floor |
largest integer <= x | [-2. -2. -1. 0. 1. 1. 2.] |
np.ceil |
smallest integer >= x | [-1. -1. -0. 1. 2. 2. 2.] |
np.trunc |
drop the fractional part (toward zero) | [-1. -1. -0. 0. 1. 1. 2.] |
np.fix |
the manual’s name for floor-towards-zero; in 2.5.1 it emitted a DeprecationWarning advising np.trunc |
[-1. -1. -0. 0. 1. 1. 2.] |
np.rint |
round to nearest integer (halves to even) | [-2. -2. -0. 0. 2. 2. 2.] |
math.floor |
Python standard library, one number at a time; for a float returned an int in this run |
math.floor(-2.5) gives -3 |
np.floor_divide (or //) |
divide and round down in one step | np.floor_divide(x, 5) on the binning example gave [0. 1. 2. 3. 3. 0.], matching np.floor(x / 5) |
The Python documentation describes math.floor(x) as returning the floor of x, the largest integer less than or equal to x, and says that if x is not a float it delegates to x.__floor__. Use math.floor for a single number and np.floor for arrays.
FAQ
What does np.floor do?
It returns, for each element, the largest integer less than or equal to that element. For a scalar input it returns a scalar.
Does np.floor round negative numbers toward zero?
No. It rounds toward minus infinity, so np.floor(-2.5) is -3. Use np.trunc (the manual’s fix) for rounding toward zero.
Why does np.floor return floats?
For float input the output stays a float type, as the -2. notation in the manual’s example shows. Call .astype(int) to get integers.
What is the difference between np.floor and math.floor?
np.floor is a NumPy ufunc that works element-wise on arrays; math.floor works on one number and delegates to __floor__ for non-float types.
Sources
- NumPy manual,
numpy.floor(v2.5): https://numpy.org/doc/stable/reference/generated/numpy.floor.html, checked 2026-10-29. - NumPy manual,
numpy.fix(deprecation note): https://numpy.org/doc/stable/reference/generated/numpy.fix.html, checked 2026-10-29. - NumPy installation: https://numpy.org/install/, checked 2026-10-29.
- Python documentation,
math.floor: https://docs.python.org/3/library/math.html, checked 2026-10-29. - Comparison table outputs,
out/where, NaN and integer-dtype behaviour: run with Python 3.14.6 and NumPy 2.5.1 on 2026-10-29.








