Open In App

Store Different Datatypes In One Numpy Array

Last Updated : 10 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Storing diverse data types in a single NumPy array presents an effective approach to handling varied datasets efficiently. Although NumPy arrays are commonly homogeneous, situations may arise where managing multiple data types within a single array becomes necessary.

In this article, we will understand How To Store Different Datatypes In One Numpy Array

Store Different datatypes in one NumPy array

Let’s see the simplest solution to store different datatypes in one NumPy array uses. NumPy to create three arrays with different data types: array1 with int32, array2 with float64, and array3 with object type using dtype. Then, the np.hstack function horizontally stacks these arrays, creating combined_array. The dtype parameter is used to explicitly set data types.

Python3




import numpy as np
 
# Create three arrays with different data types
array1 = np.array([1, 2, 3], dtype=np.int32)
array2 = np.array([1.5, 2.5, 3.5], dtype=np.float64)
array3 = np.array(['a', 'b', 'c'], dtype=object)
 
# Combine arrays horizontally using np.hstack
combined_array = np.hstack((array1, array2, array3))
print("Array 1:")
print(array1)
print("Array 2:")
print(array2)
print("Array 3:")
print(array3)
print("Combined Array:")
print(combined_array)


Output:

Array 1:
[1 2 3]
Array 2:
[1.5 2.5 3.5]
Array 3:
['a' 'b' 'c']
Combined Array:
[1 2 3 1.5 2.5 3.5 'a' 'b' 'c']

Record Arrays with dtype

In NumPy, you can use record arrays to store different data types within a single array. Record arrays allow you to define fields with different names and data types. Here’s an example:

Python3




import numpy as np
dtype = np.dtype([('integer_field', np.int32), ('float_field', np.float64), ('string_field', 'U10')])
# Create a record array with different data types
record_array = np.array([(1, 1.5, 'one'), (2, 2.5, 'two'), (3, 3.5, 'three')], dtype=dtype)
print(record_array)
print(record_array.dtype)


Output:

[(1, 1.5, 'one') (2, 2.5, 'two') (3, 3.5, 'three')]
[('integer_field', '<i4'), ('float_field', '<f8'), ('string_field', '<U10')]

Structured Arrays with dtype

Structured arrays in NumPy allow you to create arrays with compound data types, where each element has multiple fields with different data types. The dtype parameter is used to specify the data type for each field. Structured arrays are useful for representing structured data, such as records in a database Let’s see the implementation with below code:

  • dtype specifies the structured array’s data type. The field ‘keys’ has a string data type ('U1'), and the field ‘data’ has a 64-bit integer data type ('<i8').
  • The structured array is created with the specified data type.

Python3




import numpy as np
 
# Create a structured array with specified dtype
structured_array = np.array([('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)],
                            dtype=[('keys', 'U1'), ('data', '<i8')])
print("Structured Array:")
print(structured_array)
print("\nKeys Field:")
print(structured_array['keys'])
print("\nData Field:")
print(structured_array['data'])


Output:

Structured Array:
[('a', 0) ('b', 1) ('c', 2) ('d', 3) ('e', 4)]
Keys Field:
['a' 'b' 'c' 'd' 'e']
Data Field:
[0 1 2 3 4]

Conclusion

The capacity to hold many data formats within a solitary NumPy array creates novel opportunities for effectively handling heterogeneous datasets.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads