Introduction
NumPy supports a wide range of data types, making it highly flexible for scientific and numerical computing. Understanding these data types is essential for efficient data manipulation and analysis. In this chapter, you will learn various data types supported by NumPy, along with examples of how to use them.
Numeric Types
Integer Types
NumPy supports various integer types, each with different sizes and ranges. These include int8
, int16
, int32
, and int64
, representing 8-bit, 16-bit, 32-bit, and 64-bit signed integers, respectively. Unsigned integer types are also available, such as uint8
, uint16
, uint32
, and uint64
.
import numpy as np
# Integer array
int_array = np.array([1, 2, 3], dtype=np.int32)
print("Integer array:", int_array)
print("Data type:", int_array.dtype)
Output:
Integer array: [1 2 3]
Data type: int32
Floating-Point Types
NumPy supports floating-point types such as float16
, float32
, and float64
, representing 16-bit, 32-bit, and 64-bit floating-point numbers.
# Floating-point array
float_array = np.array([1.1, 2.2, 3.3], dtype=np.float64)
print("Floating-point array:", float_array)
print("Data type:", float_array.dtype)
Output:
Floating-point array: [1.1 2.2 3.3]
Data type: float64
Complex Types
NumPy also supports complex numbers with complex64
, complex128
, and complex256
, representing complex numbers with 64-bit, 128-bit, and 256-bit precision.
# Complex number array
complex_array = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128)
print("Complex number array:", complex_array)
print("Data type:", complex_array.dtype)
Output:
Complex number array: [1.+2.j 3.+4.j 5.+6.j]
Data type: complex128
Boolean Type
NumPy supports the boolean data type bool_
, which can store True
or False
values.
# Boolean array
bool_array = np.array([True, False, True], dtype=np.bool_)
print("Boolean array:", bool_array)
print("Data type:", bool_array.dtype)
Output:
Boolean array: [ True False True]
Data type: bool
String Type
NumPy supports string data types, which are represented as fixed-length strings. The str_
type can store Unicode strings.
# String array
string_array = np.array(['apple', 'banana', 'cherry'], dtype=np.str_)
print("String array:", string_array)
print("Data type:", string_array.dtype)
Output:
String array: ['apple' 'banana' 'cherry']
Data type: <U6
Object Type
The object_
data type allows you to store arbitrary Python objects in a NumPy array. This is useful for arrays containing mixed data types or custom objects.
# Object array
object_array = np.array([1, 'apple', 3.5], dtype=np.object_)
print("Object array:", object_array)
print("Data type:", object_array.dtype)
Output:
Object array: [1 'apple' 3.5]
Data type: object
Structured Data Types
NumPy allows you to define structured data types, which are useful for handling heterogeneous data. You can create structured arrays using a combination of different data types.
Example: Structured Array
# Define a structured data type
structured_dtype = np.dtype([('name', np.str_, 10), ('age', np.int32), ('weight', np.float64)])
# Create a structured array
structured_array = np.array([('Alice', 25, 55.0), ('Bob', 30, 70.5)], dtype=structured_dtype)
print("Structured array:\n", structured_array)
print("Data type:\n", structured_array.dtype)
Output:
Structured array:
[('Alice', 25, 55. ) ('Bob', 30, 70.5)]
Data type:
[('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]
Time Data Types
NumPy provides data types for handling dates and times, such as datetime64
and timedelta64
.
Example: Date and Time Arrays
# Date array
date_array = np.array(['2021-01-01', '2022-01-01'], dtype=np.datetime64)
print("Date array:", date_array)
print("Data type:", date_array.dtype)
# Time delta array
time_delta_array = np.array(['1', '2'], dtype=np.timedelta64)
print("Time delta array:", time_delta_array)
print("Data type:", time_delta_array.dtype)
Output:
Date array: ['2021-01-01' '2022-01-01']
Data type: datetime64[D]
Time delta array: [1 2]
Data type: timedelta64[D]
Complete Example
Here is a complete example demonstrating various NumPy data types.
import numpy as np
# Integer array
int_array = np.array([1, 2, 3], dtype=np.int32)
print("Integer array:", int_array)
print("Data type:", int_array.dtype)
# Floating-point array
float_array = np.array([1.1, 2.2, 3.3], dtype=np.float64)
print("Floating-point array:", float_array)
print("Data type:", float_array.dtype)
# Complex number array
complex_array = np.array([1+2j, 3+4j, 5+6j], dtype=np.complex128)
print("Complex number array:", complex_array)
print("Data type:", complex_array.dtype)
# Boolean array
bool_array = np.array([True, False, True], dtype=np.bool_)
print("Boolean array:", bool_array)
print("Data type:", bool_array.dtype)
# String array
string_array = np.array(['apple', 'banana', 'cherry'], dtype=np.str_)
print("String array:", string_array)
print("Data type:", string_array.dtype)
# Object array
object_array = np.array([1, 'apple', 3.5], dtype=np.object_)
print("Object array:", object_array)
print("Data type:", object_array.dtype)
# Structured array
structured_dtype = np.dtype([('name', np.str_, 10), ('age', np.int32), ('weight', np.float64)])
structured_array = np.array([('Alice', 25, 55.0), ('Bob', 30, 70.5)], dtype=structured_dtype)
print("Structured array:\n", structured_array)
print("Data type:\n", structured_array.dtype)
# Date array
date_array = np.array(['2021-01-01', '2022-01-01'], dtype=np.datetime64)
print("Date array:", date_array)
print("Data type:", date_array.dtype)
# Time delta array
time_delta_array = np.array(['1', '2'], dtype=np.timedelta64)
print("Time delta array:", time_delta_array)
print("Data type:", time_delta_array.dtype)
Output:
Integer array: [1 2 3]
Data type: int32
Floating-point array: [1.1 2.2 3.3]
Data type: float64
Complex number array: [1.+2.j 3.+4.j 5.+6.j]
Data type: complex128
Boolean array: [ True False True]
Data type: bool
String array: ['apple' 'banana' 'cherry']
Data type: <U6
Object array: [1 'apple' 3.5]
Data type: object
Structured array:
[('Alice', 25, 55. ) ('Bob', 30, 70.5)]
Data type:
[('name', '<U10'), ('age', '<i4'), ('weight', '<f8')]
Date array: ['2021-01-01' '2022-01-01']
Data type: datetime64[D]
Time delta array: [1 2]
Data type: timedelta64[D]
Conclusion
NumPy supports a wide range of data types, allowing you to handle various kinds of numerical, boolean, string, object, structured, and time data efficiently. Understanding these data types and how to use them is crucial for effective data manipulation and analysis in NumPy.