Labs ICT
Pro Login

Data Types

int, float, string — choosing the right dtype.

Data Types

Data types control how NumPy stores and treats your data. Think of it like choosing between storing money as cents (int) or dollars (float). The choice affects precision and memory.

Common Data Types

NumPy has many dtypes for different needs.


import numpy as np

int_arr = np.array([1, 2, 3], dtype=np.int32)
float_arr = np.array([1.5, 2.5, 3.5], dtype=np.float64)
bool_arr = np.array([True, False, True], dtype=np.bool_)

print(f"Int32: {int_arr.dtype}")
print(f"Float64: {float_arr.dtype}")
print(f"Bool: {bool_arr.dtype}")
    

Common dtypes: int8, int16, int32, int64, float32, float64, bool, complex64, complex128.

Type Casting with astype

Convert between types with astype().


import numpy as np

arr = np.array([1.7, 2.3, 3.9])
int_arr = arr.astype(np.int32)
str_arr = arr.astype(str)

print(f"Original: {arr}")
print(f"As int: {int_arr}")
print(f"As string: {str_arr}")
    

Here is the thing - astype creates a new array. The original stays unchanged.

Memory Considerations

Smaller dtypes use less memory.


import numpy as np

big = np.arange(1000000)
print(f"Int64 size: {big.nbytes / 1e6:.1f} MB")

small = big.astype(np.int8)
print(f"Int8 size: {small.nbytes / 1e6:.1f} MB")
    

One thing that confused me at first was when to use which dtype. Use the smallest that fits your data range.

Try it Yourself →

Key Takeaways

  • Dtypes control how data is stored
  • Use astype() to convert between types
  • Smaller dtypes use less memory
  • Choose the smallest dtype that fits your data