Open In App

Filter Out Integers from Float Numpy Array

Last Updated : 27 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes, a NumPy array contains both integers and floats and we want to remove only the integers while keeping the float values. This can be done in multiple ways using NumPy functions.

Using np.isclose()

This method compares each element of the array with its integer-converted version. If both values are nearly equal, element is treated as an integer and removed.

In this example, we use np.isclose() with integer conversion to identify integers and exclude them.

Python
import numpy as np

arr = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0])
print("Array:", arr)

mask = np.isclose(arr, arr.astype(int))
res = arr[~mask]
print("Result:", res)

Output
Array: [1.  1.2 2.2 2.  3.  2. ]
Result: [1.2 2.2]

Explanation:

  • arr.astype(int) converts values to integers.
  • np.isclose() checks if original and integer values are equal.
  • ~mask keeps only non-integers.

Using np.equal() and np.mod()

This method uses modular arithmetic. By dividing each element by 1 (np.mod(arr, 1)), the fractional part is extracted. If the remainder is 0, the number is an integer. We then exclude those integers.

In this example, we use np.mod() to find non-integers and filter them out.

Python
import numpy as np

arr = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0])
print("Array:", arr)

res = arr[~np.equal(np.mod(arr, 1), 0)]
print("Result:", res)

Output
Array: [1.  1.2 2.2 2.  3.  2. ]
Result: [1.2 2.2]

Explanation:

  • np.mod(arr, 1) gives fractional parts.
  • If result is 0 -> integer.
  • ~np.equal(..., 0) removes those integers.

Using astype(int)

This method casts the entire array to integers and then compares it with the original array. Wherever values match, those are integers and can be filtered out. It’s simple but may be less precise for floating-point edge cases.

In this example, we compare array elements with their integer values and filter out integers.

Python
import numpy as np

arr = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0])
print("Array:", arr)

res = arr[arr != arr.astype(int)]
print("Result:", res)

Output
Array: [1.  1.2 2.2 2.  3.  2. ]
Result: [1.2 2.2]

Explanation:

  • arr.astype(int) converts each value to an integer.
  • If original not equal to converted -> keep it.  

Using round()

This method rounds each element and compares it with the original. If both are equal, number is considered an integer and removed. However, due to floating-point precision issues, it may not always be as accurate as np.isclose().

In this example, we check if elements are different from their rounded versions and filter out integers.

Python
import numpy as np

arr = np.array([1.0, 1.2, 2.2, 2.0, 3.0, 2.0])
print("Array:", arr)

mask = np.isreal(arr)
res = arr[mask]
res = res[res != np.round(res)]
print("Result:", res)

Output
Array: [1.  1.2 2.2 2.  3.  2. ]
Result: [1.2 2.2]

Explanation:

  • np.round(arr) rounds each element.
  • If original is not equal to rounded -> float value is kept.

Explore