Numpy: Get array data type

20 August 2020

Code

# Import library
import numpy as np

# Create array
x_int = np.array([[1,2], # Integer array
              [3,4]])

x_float = np.array([[0.1,0.2], # Float array
              [0.3,0.4]])

x_str = np.array([['a','b'], # String array
              ['c','d']])

x_bool = np.array([[True,False], # Boolean array
              [True,False]])

# dtype
x_int_dt = x_int.dtype
x_float_dt = x_float.dtype
x_str_dt = x_str.dtype
x_bool_dt = x_bool.dtype

# type
x_int_t = type(x_int)
x_float_t = type(x_float)
x_str_t = type(x_str)
x_bool_t = type(x_bool)

# Output
print("x_int:\n", x_int)
print("dtype:", x_int_dt, "; type:", x_int_t, '\n')

print("x_int:\n", x_float)
print("dtype:", x_float_dt, "; type:", x_float_t, '\n')

print("x_int:\n", x_str)
print("dtype:", x_str_dt, "; type:", x_str_t, '\n')

print("x_int:\n", x_bool)
print("dtype:", x_bool_dt, "; type:", x_bool_t, '\n')
x_int:
 [[1 2]
 [3 4]]
dtype: int64 ; type: <class 'numpy.ndarray'> 

x_int:
 [[0.1 0.2]
 [0.3 0.4]]
dtype: float64 ; type: <class 'numpy.ndarray'> 

x_int:
 [['a' 'b']
 ['c' 'd']]
dtype: <U1 ; type: <class 'numpy.ndarray'> 

x_int:
 [[ True False]
 [ True False]]
dtype: bool ; type: <class 'numpy.ndarray'> 






Any errors in code above?
Please send a message.