I am wondering if there is an efficient way to get X the number of rows below and above a subset of rows. I created the base implementation below, but I'm sure there is a better way. The subset that excites me is buyindex, which is the index of rows that have a buy signal. I want to get a few lines above and below sellindex to make sure my algorithm works correctly. How can I do this in an efficient way? My path seems roundabout.
buyindex = list(data2[data2['buy'] == True].index)
print buyindex [71, 102, 103, 179, 505, 506, 607]
buyindex1 = map(lambda x: x + 1, buyindex)
buyindex2 = map(lambda x: x - 1, buyindex)
buyindex3 = map(lambda x: x - 2, buyindex)
buyindex4 = map(lambda x: x + 2, buyindex)
buyindex.extend(buyindex1)
buyindex.extend(buyindex2)
buyindex.extend(buyindex3)
buyindex.extend(buyindex4)
buyindex.sort()
data2.iloc[buyindex]
UPDATE is a data structure. I have indicators of "buys." but I basically want to get some indices above and below purchases.
VTI upper lower sell buy AboveUpper BelowLower date tokens_left
38 61.25 64.104107 61.341893 False True False True 2007-02-28 00:00:00 5
39 61.08 64.218341 61.109659 False True False True 2007-03-01 00:00:00 5
40 60.21 64.446719 60.640281 False True False True 2007-03-02 00:00:00 5
41 59.51 64.717936 60.050064 False True False True 2007-03-05 00:00:00 5
142 63.27 68.909776 64.310224 False True False True 2007-07-27 00:00:00 5
217 62.98 68.858308 63.587692 False True False True 2007-11-12 00:00:00 5
254 61.90 66.941126 61.944874 False True False True 2008-01-07 00:00:00 5
255 60.79 67.049925 61.312075 False True False True 2008-01-08 00:00:00 5
296 57.02 61.382677 57.371323 False True False True 2008-03-07 00:00:00 5
297 56.15 61.709166 56.788834 False True False True 2008-03-10 00:00:00 5
UPDATE: I created a generic function based on the selected answer. Let me know if you think this can be done even more efficiently.
def get_test_index(df, column, numbers):
"""
builds an test index based on a range of numbers above and below the a specific index you want.
df = dataframe to build off of
column = the column that is important to you. for instance, 'buy', or 'sell'
numbers = how many above and below you want of the important index
"""
idx_l = list(df[df[column] == True].index)
for i in range(numbers)[1:]:
idxpos = data2[column].shift(i).fillna(False)
idxpos = list(df[idxpos].index)
idx_l.extend(idxpos)
idxneg = data2[column].shift(-i).fillna(False)
idxneg = list(df[idxneg].index)
idx_l.extend(idxneg)
return sorted(idx_l)