Friday, September 5, 2025
HomeLanguagesNumpy MaskedArray.astype() function | Python

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)
Parameters: 
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.

Code #1 : 
 

Python3




# Python program explaining
# numpy.MaskedArray.astype() method
 
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
 
# creating input array
in_arr = geek.array([1, 2, 3, -1, 5])
print ("Input array : ", in_arr)
 
# Now we are creating a masked array of int32
# and making third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[0, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
 
# printing the data type of masked array
print(mask_arr.dtype)
 
# applying MaskedArray.astype methods to mask array
# and converting it to float64
out_arr = mask_arr.astype('float64')
print ("Output typecasted array : ", out_arr)
 
# printing the data type of typecasted masked array
print(out_arr.dtype)


Output: 

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 : 
 

Python3




# Python program explaining
# numpy.MaskedArray.astype() method
 
# importing numpy as geek
# and numpy.ma module as ma
import numpy as geek
import numpy.ma as ma
 
# creating input array
in_arr = geek.array([10.1, 20.2, 30.3, 40.4, 50.5], dtype ='float64')
print ("Input array : ", in_arr)
 
# Now we are creating a masked array by making
# first and third entry as invalid.
mask_arr = ma.masked_array(in_arr, mask =[1, 0, 1, 0, 0])
print ("Masked array : ", mask_arr)
 
# printing the data type of masked array
print(mask_arr.dtype)
 
# applying MaskedArray.astype methods to mask array
# and converting it to int32
out_arr = mask_arr.astype('int32')
print ("Output typecasted array : ", out_arr)
 
# printing the data type of typecasted masked array
print(out_arr.dtype)


Output: 

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

 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6636 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7027 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS