Numpy MaskedArray.filled() method – Python

numpy.MaskedArray.filled() function return a copy of self, with masked values filled with a given value. However, if there are no masked values to fill, self will be returned instead as an ndarray.

Syntax : numpy.MaskedArray.filled(self, fill_value = None)

Parameters :
fill_value : [scalar, optional] The value to use for invalid entries, by default is None. If None, the fill_value attribute of the array is used instead.

Return :
filled_array : [ndarray] A copy of self with invalid entries replaced by fill_value or self itself as an ndarray if there are no invalid entries to be replaced.

Code #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.filled() function
  
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
  
arr = geek.ma.array([2, 4, 6, 8, 10], mask =[0, 0, 1, 0, 1],
                                          fill_value = -999)
gfg = arr.filled()
  
print(gfg)

chevron_right


Output :

[   2    4 -999    8 -999]

 
Code #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program explaining
# numpy.MaskedArray.filled() function
  
# importing numpy as geek  
# and numpy.ma module as ma 
import numpy as geek 
import numpy.ma as ma
  
arr = geek.ma.array([1, 2, 3, 4, 5], mask =[1, 0, 1, 0, 0], 
                                         fill_value = -999)
gfg = arr.filled()
  
print(gfg)

chevron_right


Output :

[-999    2 -999    4    5]

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.