Check data type in NumPy - GeeksforGeeks

09 Aug, 2021
Numpy is a module in python. It is originally called numerical python, but in short, we pronounce it as numpy. NumPy is a general-purpose array-processing package in python. It provides high-performance multidimensional data structures like array objects and tools for working with these arrays. Numpy provides faster and efficient calculations of matrices and arrays.
NumPy provides familiarity with almost all mathematical functions. In numpy these functions are called universal function ufunc.
Method #1
Checking datatype using dtype.
Example 1:
Python3
# importing numpy libraryimport numpy as np # creating and initializing an arrayarr = np.array([1, 2, 3, 23, 56, 100]) # printing the array and checking datatypeprint('Array:', arr) print('Datatype:', arr.dtype)
Output:
Array: [  1   2   3  23  56 100]
Datatype: int32
Example 2:
Python3
import numpy as np # creating and initializing array of stringarr_1 = np.array(['apple', 'ball', 'cat', 'dog']) # printing array and its datatypeprint('Array:', arr_1) print('Datatype:', arr_1.dtype)
Output:
Array: ['a' 'b' 'c' 'd']
Datatype: <U1
Method #2
Creating the array with a defined datatype. Creating numpy array by using an array function array(). This function takes argument dtype that allows us to define the expected data type of the array elements:
Example 1:
Python3
import numpy as np # Creating and initializing array with datatypearr = np.array([1, 2, 3, 8, 7, 5], dtype='S') # printing array and its datatypeprint("Array:", arr)print("Datatype:", arr.dtype)
Output:
Array: [b'1' b'2' b'3' b'8' b'7' b'5']
Datatype: |S1
S is used for defining string datatype. We use i, u, f, S and U for defining various other data types along with their size.
Example 2:
Python3
import numpy as np # creating and initialising array along# with datatype and its size 4 i.e. 32bytesarr = np.array([1, 2, 3, 4], dtype='i4') # printing array and datatypeprint('Array:', arr)print('Datatype:', arr.dtype)
Output:
Array: [1 2 3 4 8 9 5]
Datatype: int32
In the above example, the size of integer elements is 4 i.e. 32bytes
Example 3:
Python3
import numpy as np # creating and initialising array along# with datatype and its size 8 i.e. 64bytesarr = np.array([1, 2, 3, 4], dtype='i8') # printing array and datatypeprint('Array:', arr)print('Datatype:', arr.dtype)
Output:
Array: [1 2 3 4 8 9 7]
Datatype: int64
And in this example the size of elements is 64bytes.
Example 4:
Python3
import numpy as np # creating and initialising array along# with datatype and its size 4 i.e. 32bytesarr = np.array([1, 2, 3, 4, 8, 9, 7], dtype='f4') # printing array and datatypeprint('Array:', arr)print('Datatype:', arr.dtype)
Output:
Array: [1. 2. 3. 4. 8. 9. 7.]
Datatype: float32
In the above example, the data type is float and the size is 32bytes.
Example 5:
Python3
import numpy as np # creating and initialising array along# with datatype and its size 2arr = np.array([1, 2, 3, 4, 8, 9, 7], dtype='S2') # printing array and datatypeprint('Array:', arr)print('Datatype:', arr.dtype)
Output:
Array: [b'1' b'2' b'3' b'4' b'8' b'9' b'7']
Datatype: |S2
In the above example, the datatype is a string and the size is 2.
anikakapoor
Python numpy-DataType
Python-numpy
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Check if element exists in list in Python
Selecting rows in pandas DataFrame based on conditions
Python | os.path.join() method
Defaultdict in Python
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()