I am struggling to find a Numpy equivalent for a particular Matlab coding "pattern" using ismember.
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.
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.
For concreteness say I have quarterly GDP data that I map onto a monthly time grid as follows.
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);
Note that not all the quarters appear in the list of months so both the tf and the loc variables are required.
I have seen similar questions on StackOverflow but they either just give a pure Python solution (here) or where numpy is used then the loc argument isn't returned (here).
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.
Comments or redesign suggestions are also welcome.