Numpy.ma not always necessary

I just discovered that there is an easier way to do this (e.g. from tutorial06):

import numpy.ma as ma
mask = ma.masked_where(countries['ISO'] != iso, countries['ISO'])
country = ma.array(countries['country'],mask=mask.mask).compressed()[0]

by using the built-in numpy.where method:

import numpy as np
index = np.where(countries['ISO'] == iso)
country = countries['country'][index][0]

Yeah, that’s fun !

The numpy.where method takes two extra args, the “value if true”,”value if false”, very handy !

One thought on “Numpy.ma not always necessary

  1. Pingback: Tweets that mention Numpy.ma not always necessary - Géophysique.be -- Topsy.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*