NumPy
Python
array conversion
data types
2D arrays

Convert 2D float array to 2D int array in NumPy

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Converting a 2D float array to integers in NumPy is straightforward, but the right approach depends on what should happen to the fractional part. The main mistake is assuming that every conversion rounds values, preserves special values, or updates the original array in place.

The Direct Conversion: astype

The most common solution is astype. It creates a new array with the requested integer type.

python
1import numpy as np
2
3floats = np.array([
4    [1.9, 2.1, -3.7],
5    [4.0, 5.8, 6.2]
6], dtype=np.float64)
7
8ints = floats.astype(np.int64)
9
10print(ints)
11print(ints.dtype)

Output:

text
[[ 1  2 -3]
 [ 4  5  6]]
int64

astype truncates toward zero when converting floating-point values to integers. That means:

  • '1.9 becomes 1'
  • '-3.7 becomes -3'

If truncation is exactly what you want, this is the cleanest solution.

Choose The Right Integer Behavior

Many questions about this conversion are really questions about rounding policy. NumPy gives you several vectorized options before the final cast.

Truncate Toward Zero

python
ints = floats.astype(np.int32)

Use this when the decimal portion is irrelevant and you want C-like numeric conversion behavior.

Round To The Nearest Integer

python
rounded = np.rint(floats).astype(np.int64)
print(rounded)

np.rint rounds to the nearest integer value using NumPy's floating-point rounding rules. This is usually the right choice when the float values represent measurements or computed scores that should be rounded instead of chopped.

Always Round Down Or Up

python
1floored = np.floor(floats).astype(np.int64)
2ceiled = np.ceil(floats).astype(np.int64)
3
4print(floored)
5print(ceiled)

This matters when negative values are involved. astype and np.floor do not behave the same way for negative numbers:

  • 'astype turns -3.7 into -3'
  • 'np.floor turns -3.7 into -4'

That difference is often the real source of bugs.

Preserving Shape In A 2D Array

A 2D NumPy array keeps its shape during conversion. Only the element type changes.

python
1import numpy as np
2
3matrix = np.array([
4    [10.2, 11.5],
5    [12.9, 13.0],
6    [14.1, 15.6]
7])
8
9converted = matrix.astype(np.int64)
10
11print(matrix.shape)
12print(converted.shape)

Both arrays have the same dimensions. If your result stops being 2D, the problem is usually elsewhere, such as indexing with a single row or flattening the data before conversion.

Picking A Safe Integer Type

int is not a single universal size in every environment, so it is better to be explicit when the data range matters. Common choices are:

  • 'np.int32 for smaller values and lower memory use'
  • 'np.int64 for larger ranges'

For example:

python
1import numpy as np
2
3data = np.array([[1000.4, 2000.9]], dtype=np.float64)
4
5as_int32 = data.astype(np.int32)
6as_int64 = data.astype(np.int64)
7
8print(as_int32.dtype, as_int64.dtype)

When you are sending data into another library, matching the expected type is often more important than saving a few bytes.

Handling NaN And Infinity

If your float array may contain NaN, positive infinity, or negative infinity, cast carefully. Integer arrays cannot represent those values.

python
1import numpy as np
2
3floats = np.array([
4    [1.2, np.nan],
5    [np.inf, -2.8]
6])
7
8clean = np.nan_to_num(floats, nan=0.0, posinf=9999.0, neginf=-9999.0)
9ints = clean.astype(np.int64)
10
11print(ints)

Sanitizing first makes the conversion rule explicit. That is much better than discovering later that bad numeric values polluted downstream logic.

Performance Notes

NumPy conversions are already vectorized, so you should almost never convert element by element with Python loops. This:

python
ints = floats.astype(np.int64)

is faster, shorter, and less error-prone than iterating through rows and columns manually.

If memory pressure matters, remember that astype typically returns a new array. The original float array is still in memory unless it is released.

Common Pitfalls

  • Expecting astype to round. It truncates toward zero.
  • Forgetting that negative numbers behave differently under truncation and floor.
  • Assuming the original array changed. astype usually returns a new array.
  • Casting arrays that contain NaN or infinity without cleaning them first.
  • Using a too-small integer type and silently overflowing large values.

Summary

  • Use astype for a direct float-to-int conversion in NumPy.
  • Decide whether you want truncation, rounding, floor, or ceil before casting.
  • A 2D array stays 2D after conversion unless another operation changes the shape.
  • Prefer explicit types such as np.int32 or np.int64 when range matters.
  • Clean NaN and infinity values before converting to integers.

Course illustration
Course illustration

All Rights Reserved.