# Return the scalar dtype or NumPy equivalent of Python type of an object

To return the scalar dtype or NumPy equivalent of Python type of an object, use the numpy.obj2sctype() method. The 1st parameter is the object of which the type is returned The default parameter, if given, is returned for objects whose types cannot be determined. If not given, None is returned for those objects.

## Steps

At first, import the required library −

import numpy as np

To return the scalar dtype or NumPy equivalent of Python type of an object, use the numpy.obj2sctype() method −

print("Using the obj2sctype() method in Numpy")

Checking for int −

print("Result...",np.obj2sctype(np.array([45, 89])))
print("Result...",np.obj2sctype(np.array([389, 7985])))

Checking for float −

print("Result...",np.obj2sctype(np.float32))
print("Result...",np.obj2sctype(np.float64))
print("Result...",np.obj2sctype(np.array([5., 25., 40.])))

Checking for complex −

print("Result...",np.obj2sctype(np.array([5.6j]))

## Example

import numpy as np

# To return the scalar dtype or NumPy equivalent of Python type of an object, use the numpy.obj2sctype() method.
# The 1st parameter is the object of which the type is returned
# The default parameter, if given, is returned for objects whose types cannot be determined.
# If not given, None is returned for those objects.
print("Using the obj2sctype() method in Numpy")

# checking for int
print("Result...",np.obj2sctype(np.array([45, 89])))
print("Result...",np.obj2sctype(np.array([389, 7985])))

# checking for float
print("Result...",np.obj2sctype(np.float32))
print("Result...",np.obj2sctype(np.float64))
print("Result...",np.obj2sctype(np.array([5., 25., 40.])))

# checking for complex
print("Result...",np.obj2sctype(np.array([5.6j])))

## Output

Using the obj2sctype() method in Numpy

Result... <class 'numpy.int64'>
Result... <class 'numpy.int64'>
Result... <class 'numpy.float32'>
Result... <class 'numpy.float64'>
Result... <class 'numpy.float64'>
Result... <class 'numpy.complex128'>

Updated on: 24-Feb-2022

127 Views 