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 1058219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:59:34+00:00 2026-05-16T17:59:34+00:00

The numpy documentation shows an example of masking existing values with ma.masked a posteriori

  • 0

The numpy documentation shows an example of masking existing values with ma.masked a posteriori (after array creation), or creating a masked array from an list of what seem to be valid data types (integer if dtype=int). I am trying to read in data from a file (and requires some text manipulation) but at some point I will have a list of lists (or tuples) containing strings from which I want to make a numeric (float) array.

An example of the data might be textdata='1\t2\t3\n4\t\t6' (typical flat text format after cleaning).

One problem I have is that missing values may be encoded as ”, which when trying to convert to float using the dtype argument, will tell me

ValueError: setting an array element with a sequence. 

So I’ve created this function

def makemaskedarray(X,missing='',fillvalue='-999.',dtype=float):
    arr = lambda x: x==missing and fillvalue or x    
    mask = lambda x: x==missing and 1 or 0
    triple = dict(zip(('data','mask','dtype'),
                      zip(*[(map(arr,x),map(mask,x)) for x in X])+
                      [dtype]))
    return ma.array(**triple)

which seems to do the trick:

>>> makemaskedarray([('1','2','3'),('4','','6')])
masked_array(data =
 [[1.0 2.0 3.0]
 [4.0 -- 6.0]],
             mask =
 [[False False False]
 [False  True False]],
       fill_value = 1e+20)

Is this the way to do it? Or there is a built-in function?

  • 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-16T17:59:34+00:00Added an answer on May 16, 2026 at 5:59 pm

    The way you’re doing it is fine. (though you could definitely make it a bit more readable by avoiding building the temporary “triple” dict, just to expand it a step later, i.m.o.)

    The built-in way is to use numpy.genfromtxt. Depending on the amount of pre-processing you need to do to your text file, it may or may not do what you need. However, as a basic example: (Using StringIO to simulate a file…)

    from StringIO import StringIO
    import numpy as np
    
    txt_data = """
    1\t2\t3
    4\t\t6
    7t\8t\9"""
    
    infile = StringIO(txt_data)
    data = np.genfromtxt(infile, usemask=True, delimiter='\t')
    

    Which yields:

    masked_array(data =
     [[1.0 2.0 3.0]
     [4.0 -- 6.0]
     [7.0 8.0 9.0]],
                 mask =
     [[False False False]
     [False  True False]
     [False False False]],
           fill_value = 1e+20)
    

    One word of caution: If you do use tabs as your delimiter and an empty string as your missing value marker, you’ll have issues with missing values at the start of a line. (genfromtxt essentially calls line.strip().split(delimiter)). You’d be better off using something like "xxx" as a marker for missing values, if you can.

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

Sidebar

Related Questions

I want to retrieve k max values from each row in a numpy array.
The numpy.equal function does not work if a list or array contains strings: >>>
I'm creating a numpy array which is to be filled with objects of a
I have a numpy array with positive and negative values in. a = array([1,1,-1,-2,-3,4,5])
I have a NumPy array of values. I want to count how many of
Borrowing from the example on the Matplotlib documentation page and slightly modifying the code,
For a minimal working example, let's digitize a 2D array. numpy.digitize requires a 1D
import numpy a = numpy.array([20090913, 20101020, 20110125]) Can you explain why numpy.datetime64(a.astype(S8).tolist()) converts correctly
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a numpy array like this a = np.array(1) Now if I want

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.