Hello Peter and Pumplerod,
The code I think you are trying to sort out is:
opens = history(30,'1d','open_price',ffill=False).as_matrix(context.stocks)[1:,:]
closes = history(30,'1d','close_price',ffill=False).as_matrix(context.stocks)[0:-1,:]
gap = pd.DataFrame(opens - closes).as_matrix()
The idea is to subtract the closing prices from the opening ones. So, the slicing [1:,:] of the opens eliminates the first row, which corresponds to the oldest open. And the slicing [0:-1,:] of the closes eliminates the last row, which corresponds to the most recent close. So, when the closes are subtracted from the opens, elementwise, the resulting matrix is the gap between the open, and the prior close, for each day. So, for example, the last row in gap will correspond to the opening price for the day minus the closing price for the prior day.
By the way, this will work, too:
gap = opens - closes
I think that originally I'd used a pandas function to filter out NaN's, but as it turned out, I dropped the approach.
Overall, the code I posted was kinda hacked together, and surely can be improved, including perhaps using the ".ix" notation Alisa refers to.
Hope this helps.