NumPy Data Types
NumPy Data Types
Dark mode
Dark code
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP BOOTSTRAP HOW TO W3.CSS C C++ C# REACT R JQUERY DJANGO
NumPy HOME
NumPy Intro
NumPy Getting Started
Random Intro
Data Distribution
Random Permutation Data Types in NumPy
Seaborn Module
NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.
Normal Distribution
Binomial Distribution Below is a list of all data types in NumPy and the characters used to represent them.
i - integer
b - boolean
u - unsigned integer
f - float
c - complex float
m - timedelta
M - datetime
O - object
S - string
U - unicode string
V - fixed chunk of memory for other type ( void )
COLOR PICKER
Checking the Data Type of an Array
The NumPy array object has a property called dtype that returns the data type of the array:
import numpy as np
print(arr.dtype)
Try it Yourself »
Example
Get the data type of an array containing strings:
import numpy as np
print(arr.dtype)
Try it Yourself »
ADVERTISEMENT
ADVERTISEMENT
Example
Create an array with data type string:
import numpy as np
print(arr)
print(arr.dtype)
Try it Yourself »
Example
Create an array with data type 4 bytes integer:
import numpy as np
print(arr)
print(arr.dtype)
Try it Yourself »
ValueError: In Python ValueError is raised when the type of passed argument to a function is unexpected/incorrect.
Example
A non integer string like 'a' can not be converted to integer (will raise an error):
import numpy as np
Try it Yourself »
The astype() function creates a copy of the array, and allows you to specify the data type as a parameter.
The data type can be specified using a string, like 'f' for float, 'i' for integer etc. or you can use the data type directly like
float for float and int for integer.
Example
Change data type from float to integer by using 'i' as parameter value:
import numpy as np
newarr = arr.astype('i')
print(newarr)
print(newarr.dtype)
Try it Yourself »
Example
Change data type from float to integer by using int as parameter value:
import numpy as np
newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)
Try it Yourself »
Example
Change data type from integer to boolean:
import numpy as np
newarr = arr.astype(bool)
print(newarr)
print(newarr.dtype)
Try it Yourself »
Exercise:
NumPy uses a character to represent each of the following data types, which one?
i = integer
= boolean
= unsigned integer
= float
= complex float
= timedelta
= datetime
= object
= string
Submit Answer »
ADVERTISEMENT
ADVERTISEMENT
FORUM | ABOUT
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.