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

NumPy Floor (np.floor): Round Down Arrays in Python

Learn how numpy.floor rounds arrays down, why -1.5 becomes -2.0, how it differs from ceil, trunc and math.floor, and how to bin measurements with it.

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) * width returns the lower edge of the bin of width width that contains each value, and np.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, then conda activate my-env, then conda install numpy
  • virtual environment: python -m venv my-env, then source my-env/bin/activate (macOS/Linux) or my-env\Scripts\activate (Windows), then pip 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.

  1. Import NumPy. import numpy as np
  2. Build the array. a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
  3. Call the function. np.floor(a)
  4. Read the result. array([-2., -2., -1., 0., 1., 1., 2.])
  5. Optional: convert to integers. np.floor(a).astype(int) returns array([-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 fix in 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 an int64 array [1 2 3].
  • Negative zero. The sibling functions ceil, trunc and rint printed -0. for inputs between -1 and 0 in the comparison below; this is a display of the sign of zero and compares equal to 0..

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'])) raised TypeError: 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.ceil on 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

Follow CASRAI

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

Ask CASRAI · free to try

Ask about NumPy Floor (np.floor): Round Down Arrays in Python

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.