title: Equivalent of Matlab 'ismember' in numpy (Python)?
body: <p>I am struggling to find a Numpy equivalent for a particular Matlab coding "pattern" using <a href="http://www.mathworks.com/help/techdoc/ref/ismember.html" rel="nofollow noreferrer">ismember</a>.</p>
  
  <p>Unfortunately this code tends to be where most of the time is spent in my Matlab scripts so I want to find an efficient Numpy equivalent.</p>
  
  <p>The basic pattern consists of mapping a subset onto a larger grid. I have a set of key value pairs stored as parallel arrays and I want to insert these values into a larger list of key value pairs stored in the same way.</p>
  
  <p>For concreteness say I have quarterly GDP data that I map onto a monthly time grid as follows.</p>
  
  <pre><code>quarters = [200712 200803 200806 200809 200812 200903];
  gdp_q = [10.1 10.5 11.1 11.8 10.9 10.3];
  months = 200801 : 200812;
  gdp_m = NaN(size(months));
  [tf, loc] = ismember(quarters, months);
  gdp_m(loc(tf)) = gdp_q(tf);
  </code></pre>
  
  <p>Note that not all the quarters appear in the list of months so both the <em>tf</em> and the <em>loc</em> variables are required.</p>
  
  <p>I have seen similar questions on StackOverflow but they either just give a pure Python solution (<a href="https://stackoverflow.com/questions/3824778/equivalent-of-ismember-from-matlab-in-python">here</a>) or where numpy is used then the <em>loc</em> argument isn't returned (<a href="https://stackoverflow.com/questions/1273041/how-can-i-implement-matlabs-ismember-command-in-python">here</a>).</p>
  
  <p>In my particular application area, this particular code pattern tends to arise over and over again and uses up most of the CPU time of my functions so an efficient solution here is really crucial for me. </p>
  
  <p>Comments or redesign suggestions are also welcome.</p>