Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6100201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:21:32+00:00 2026-05-23T13:21:32+00:00

import numpy as np import numpy.ma as ma This operates as expected with one

  • 0
import numpy as np
import numpy.ma as ma

"""This operates as expected with one value masked"""
a = [0., 1., 1.e20, 9.]
error_value = 1.e20
b = ma.masked_values(a, error_value)
print b

"""This does not, all values are masked """
d = [0., 1., 'NA', 9.]
error_value = 'NA'
e = ma.masked_values(d, error_value)
print e

How can I use ‘nan’, ‘NA’, ‘None’, or some similar value to indicate missing data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T13:21:32+00:00Added an answer on May 23, 2026 at 1:21 pm

    Are you getting your data from a text file or similar? If so, I’d suggest using the genfromtxt function directly to specify your masked value:

    In [149]: f = StringIO('0.0, 1.0, NA, 9.0')
    
    In [150]: a = np.genfromtxt(f, delimiter=',', missing_values='NA', usemask=True)
    
    In [151]: a
    Out[151]:
    masked_array(data = [0.0 1.0 -- 9.0],
                 mask = [False False  True False],
           fill_value = 1e+20)
    

    I think the problem in your example is that the python list you’re using to initialize the numpy array has heterogeneous types (floats and a string). The values are coerced to a strings in a numpy array, but the masked_values function uses floating point equality yielding the strange results.

    Here’s one way to overcome this by creating an array with object dtype:

    In [152]: d = np.array([0., 1., 'NA', 9.], dtype=object)
    
    In [153]: e = ma.masked_values(d, 'NA')
    
    In [154]: e
    Out[154]:
    masked_array(data = [0.0 1.0 -- 9.0],
                 mask = [False False  True False],
           fill_value = ?)
    

    You may prefer the first solution since the result has a float dtype.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import numpy as np a = np.arange(1000000).reshape(1000,1000) print(a**2) With this code I get this
I use the latest version of numpy/scipy. The following script does not work: import
I am using matplotlib for doing this from mpl_toolkits.mplot3d import Axes3D import numpy as
What's wrong with this snippet of code? import numpy as np from scipy import
why do the following lines not work as I expect? import numpy as np
This is continued from this thread: Python matrix, any solution? Input from numpy import
SciPy appears to provide most (but not all [1]) of NumPy's functions in its
import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
import numpy from numpy import asarray Initial = numpy.asarray [2.0, 4.0, 5.0, 3.0, 5.0,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.