Search code examples
pythonnumpyintegermasking

converting a list of strings into integers in python, skipping masked terms


Let's say I have a list of strings:

fnew:

masked_array(data = [-- -- '56527.9529' '56527.9544' '109.7147' '0.0089' '14.3638' '0.0779'
 '14.3136' '0.0775' '14.3305' '0.1049' '14.3628' '0.0837' '14.3628'
 '0.0837' '70.9990' '40.0050' '173.046' '-30.328' '73' '-99.175' '0.000'
 '0.000' '59.8' '0.0' '1.0'],

mask = [ True  True False False False False False False False False False False
 False False False False False False False False False False False False
 False False False],
       fill_value = N/A)

How do I get rid of the quotes from other elements, that is converting the other numbers into integer values so that I can do calculations with them?


Solution

  • Something like this:

    >>> import numpy as np
    >>> a = ['Foo', '59.8', 'bar', 'spam']
    >>> arr = np.ma.array(a, mask=[True, False, True, True])
    >>> arr.compressed().astype(float)
    array([ 59.8])
    >>> arr[arr.mask].data
    array(['Foo', 'bar', 'spam'], 
          dtype='|S4')