Skip to content

Data Types and Casting

Let's understand the different data types available in NumPy and how to cast between them, to ensure that my data is in the correct format for my analysis and computations.

Data Types

NumPy provides a range of data types, including:

  • Integer data types: int8, int16, int32, int64
  • Floating-point data types: float16, float32, float64
  • Complex data types: complex64, complex128

Each data type has its own strengths and weaknesses, and choosing the correct data type is important for ensuring accurate results

Examples of data types and casting:

import numpy as np

# Create a NumPy array with integer values
array = np.array([1, 2, 3, 4, 5], dtype=np.int32)

# Print the data type of the array
print("Data Type: ", array.dtype)

# Cast the array to a float64 array
float_array = array.astype(np.float64)

# Print the data type of the float array
print("Float Data Type: ", float_array.dtype)

# Create a NumPy array with floating-point values
float_array = np.array([1.0, 2.0, 3.0, 4.0, 5.0], dtype=np.float32)

# Print the data type of the float array
print("Float Data Type: ", float_array.dtype)

# Cast the float array to an int32 array
int_array = float_array.astype(np.int32)

# Print the data type of the int array
print("Int Data Type: ", int_array.dtype)

Here's a real-world example:

Suppose we have a dataset of exam scores for a class of students, and we want to calculate the average score and the standard deviation of the scores. We can use NumPy to create an array with the scores and perform these calculations.

import numpy as np

# Create a NumPy array with exam scores
scores = np.array([85, 90, 78, 92, 88, 76, 95, 89, 91, 82])

# Print the data type of the array
print("Data Type: ", scores.dtype)

# Calculate the average score
average_score = np.mean(scores)

# Calculate the standard deviation of the scores
std_dev = np.std(scores)

print("Average Score: ", average_score)
print("Standard Deviation: ", std_dev)

In this example, we create a NumPy array scores with the exam scores, and we print the data type of the array using the dtype attribute. We then calculate the average score and the standard deviation of the scores using the np.mean() and np.std() functions, respectively.

The output will be:

Data Type:  int64
Average Score:  86.5
Standard Deviation:  6.149
As you can see, the data type of the array is int64, which means that the array can store 64-bit integer values. However, when we calculate the average score, the result is a floating-point number, which requires a different data type.

To handle this situation, NumPy uses a process called casting, which converts the data type of the array to a compatible type that can store the result. In this case, the np.mean() function casts the int64 array to a float64 array, which can store the floating-point result.

key concepts

Here are some key concepts to keep in mind when working with data types and casting in NumPy:

  • Implicit casting: NumPy performs implicit casting when an operation requires a different data type than the original array. For example, when we calculate the average score, NumPy implicitly casts the int64 array to a float64 array.
  • Explicit casting: We can use the astype() method to explicitly cast an array to a different data type. For example, we can cast the scores array to a float64 array using scores.astype(np.float64).
  • Data type hierarchy: NumPy has a data type hierarchy that determines the order in which data types are cast. The hierarchy is as follows:

  • bool

  • int8, int16, int32, int64
  • uint8, uint16, uint32, uint64
  • float16, float32, float64
  • complex64, complex128