How to Convert NumPy Array of Floats into Integers


You can use the following methods to convert a NumPy array of floats to an array of integers:

Method 1: Convert Floats to Integers (Rounded Down)

rounded_down_integer_array = float_array.astype(int)

Method 2: Convert Floats to Integers (Rounded to Nearest Integer)

rounded_integer_array = (np.rint(some_floats)).astype(int)

Method 3: Convert Floats to Integers (Rounded Up)

rounded_up_integer_array = (np.ceil(float_array)).astype(int)

Method 4: Convert Floats to Integers Using Floor Function (Rounded Down)

floor_integer_array = (np.floor(float_array)).astype(int)

Method 5: Convert Floats to Integers Using Round Function (Rounded to Nearest Integer)

rounded_integer_array = np.round(float_array).astype(int)

The following examples show how to use each method in practice with the following NumPy array of floats:

import numpy as np

#create NumPy array of floats
float_array = np.array([2.33, 4.7, 5.1, 6.2356, 7.88, 8.5])

#view array
print(float_array)

[2.33   4.7    5.1    6.2356 7.88   8.5   ]

#view dtype of array
print(float_array.dtype)

float64

Example 1: Convert Floats to Integers (Rounded Down)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded down to the nearest integer:

#convert NumPy array of floats to array of integers (rounded down)
rounded_down_integer_array = float_array.astype(int)

#view array
print(rounded_down_integer_array)

[2 4 5 6 7 8]

#view dtype of array
print(rounded_down_integer_array.dtype)

int64

Notice that each float has been rounded down to the nearest integer and the new array has a dtype of int64.

Example 2: Convert Floats to Integers (Rounded to Nearest Integer)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded to the nearest integer:

#convert NumPy array of floats to array of integers (rounded to nearest)
rounded_integer_array = (np.rint(float_array)).astype(int)

#view array
print(rounded_integer_array)

[2 5 5 6 8 8]

#view dtype of array
print(rounded_integer_array.dtype)

int64

Notice that each float has been rounded to the nearest integer and the new array has a dtype of int64.

Example 3: Convert Floats to Integers (Rounded Up)

The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded up to the nearest integer:

#convert NumPy array of floats to array of integers (rounded up)
rounded_up_integer_array = (np.ceil(float_array)).astype(int)

#view array
print(rounded_up_integer_array)

[3 5 6 7 8 9]

#view dtype of array
print(rounded_up_integer_array.dtype)

int64

Notice that each float has been rounded up to the nearest integer and the new array has a dtype of int64.

Example 4: Convert Floats to Integers Using Floor Function (Rounded Down)

The following code shows how to convert a NumPy array of floats to an array of integers using the np.floor() function, which rounds each value down to the nearest integer:

#convert NumPy array of floats to array of integers using floor function
floor_integer_array = (np.floor(float_array)).astype(int)

#view array
print(floor_integer_array)

[2 4 5 6 7 8]

#view dtype of array
print(floor_integer_array.dtype)

int64

Notice that using np.floor() produces the same results as using astype(int) directly. Both methods truncate the decimal part of each float. The resulting array has a dtype of int64.

Example 5: Convert Floats to Integers Using Round Function (Rounded to Nearest Integer)

The following code shows how to convert a NumPy array of floats to an array of integers using the np.round() function:

#convert NumPy array of floats to array of integers using round function
rounded_integer_array = np.round(float_array).astype(int)

#view array
print(rounded_integer_array)

[2 5 5 6 8 8]

#view dtype of array
print(rounded_integer_array.dtype)

int64

Notice that np.round() gives results similar to np.rint(). The new array has a dtype of int64.

Wrapping Up

Converting NumPy arrays of floats to integers is a common task in data analysis and scientific computing. Here’s a quick summary of the methods we covered:

  • astype(int): Truncates decimal places (rounds down) at the moment of conversion to integers. 
  • np.rint(): Rounds each float to the nearest integer (using half-to-even banker’s rounding) before converting to integers. 
  • np.ceil(): Rounds each float up to the nearest integer before converting.
  • np.floor(): Explicitly rounds each float down (similar to astype(int)) before converting. 
  • np.round(): Also uses half-to-even rounding by default (same as np.rint()), with the option to specify the number of decimals if needed, then converts to integers. 

When selecting a conversion method, choose based on how you need decimal values handled. Use astype(int) or np.floor() when truncation is acceptable, np.rint() or np.round() when rounding to nearest integer is desired, and np.ceil() if rounding up is desired. 

Additional Resources

The following tutorials explain how to perform other common tasks in NumPy:

How to Fill NumPy Array with Values
How to Remove Specific Elements from NumPy Array
How to Replace Elements in NumPy Array
How to Get Specific Row from NumPy Array

Leave a Reply

Your email address will not be published. Required fields are marked *