29 Mar Datatypes in Numpy
NumPy provides a wide range of specialized data types (dtypes) beyond Python’s built-in types, such as int64, float32, complex128, and more. These dtypes allow precise control over memory usage and numerical precision, which is critical for scientific computing and large datasets.
Python Numpy supports the following data types:
- b – boolean
- u – unsigned integer
- f – float
- c – complex float
- m – timedelta
- M – datetime
- O – object
- S – string
- U – unicode string
Let us see some examples:
- Get the datatype of a NumPy array with integers
- Get the datatype of a NumPy array with strings
- Set the datatype size within a NumPy array
Convert one datatype to another
- Convert string to integer
- Convert an integer to a float
- Convert float to integer
- Convert an integer to a string
- Convert integers to booleans
1. Get the datatype of a Numpy array with integers
To get the datatype of a Numpy array, use the dtype attribute. Let us see an example:
import numpy as np n = np.array([10, 20, 30]) print(n.dtype)
Output
int64
2. Get the datatype of a Numpy array with strings
To get the datatype of a Numpy array, use the dtype attribute. Let us see an example:
import numpy as np n = np.array(["amit", "rohit", "ashwin", "jadeja", "virat"]) print(n.dtype)
Output
<U6
3. Set the datatype size within a Numpy array
Set a defined data type to a new array using the dtype attribute. With that set the size as well. Let us see an example:
# Set the datatype size within a NumPy array import numpy as np n = np.array(["John", "Doe", "Jacob", "MarkWaugh"], dtype="S5") print(n) print(n.dtype)
Output
|S5
In the above example,
- Since the datatype is limited to 5 characters, any string longer than 5 characters will be cut off after the 5th character.
"John"→"John"(fits within 5)"Doe"→"Doe"(fits within 5)"Jacob"→"Jacob"(exactly 5)"MarkWaugh"→"MarkW"(truncated to 5)
Convert one datatype to another
Use the astype() to create a copy of an array and then set the new data type. This is how you can convert one type to another.
4. Convert string to integer in Python NumPy
Let us see an example to convert string to integer:
# Convert one datatype to another in Python NumPy
# Convert string to integer
import numpy as np
n = np.array(["10", "20", "30", "40"])
print(n)
print(n.dtype)
# Using NumPy integer type code i
n2 = n.astype('i')
print(n2)
print(n2.dtype)
Output
['10' '20' '30' '40']
5. Convert integer to floats in Python NumPy
Let us see an example to convert integer to floats:
# Convert one datatype to another in Python NumPy # Convert integer to floats import numpy as np n = np.array([1, 2, 3, 4]) float_res = n.astype(float) print(n) print(n.dtype) print(float_res) print(float_res.dtype)
Output
[1 2 3 4] int64 [1. 2. 3. 4.] float64
6. Convert float to integer in Python NumPy
Let us see an example to convert float to integer:
# Convert one datatype to another in Python NumPy # Convert float to integer import numpy as np n = np.array([1.6, 2.4, 3.9, 4.2]) int_res = n.astype(int) print(n) print(n.dtype) print(int_res) print(int_res.dtype)
Output
[1.6 2.4 3.9 4.2] float64 [1 2 3 4] int64
7. Convert integer to string in Python NumPy
Let us see an example to convert integer to string:
# Convert one datatype to another in Python NumPy # Convert integer to string import numpy as np n = np.array([100, 200, 300, 400]) str_res = n.astype(str) print(n) print(n.dtype) print(str_res) # The output is
Output
[100 200 300 400] int64 ['100' '200' '300' '400']
8. Convert integer to booleans in Python NumPy
Let us see an example to convert integer to booleans:
# Convert one datatype to another in Python NumPy # Convert integers to booleans import numpy as np n = np.array([0, 1, 2, 3]) bool_res = n.astype(bool) print(n) print(n.dtype) print(bool_res) print(bool_res.dtype)
Output
[0 1 2 3] int64 [False True True True] bool
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments