Numpy MaskedArray.astype() function | Python
In many circumstances, datasets can be incomplete or tainted by the presence of invalid data. For example, a sensor may have failed to record a data, or recorded an invalid value. The numpy.ma module provides a convenient way to address this issue, by introducing masked arrays.Masked arrays are arrays that may have missing or invalid entries.
numpy.MaskedArray.astype() function returns a copy of the MaskedArray cast to given newtype.
Syntax : numpy.MaskedArray.astype(newtype)
newtype : Type in which we want to convert the masked array.
Return : [MaskedArray] A copy of self cast to input newtype. The returned record shape matches self.shape.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Code #1 :
Input array : [ 1 2 3 -1 5] Masked array : [1 2 -- -1 5] int32 Output typecasted array : [1.0 2.0 -- -1.0 5.0] float64
Code #2 :
Input array : [10.1 20.2 30.3 40.4 50.5] Masked array : [-- 20.2 -- 40.4 50.5] float64 Output typecasted array : [-- 20 -- 40 50] int32