4

I am trying to count the number of times the current row's value for a specific column 'df1' falls between the low-high range values in the previous 5 rows (in 2 side by side columns). This is a follow-up question - Dickster has already done the heavy lifting here.

The Series().between() method is not cooperating, complaining that AttributeError: 'Series' object has no attribute 'columns'. I don't understand how I am involving the columns attribute.

list1 = [[21,101],[22,110],[25,113],[24,112],[21,109],[28,108],[30,102],[26,106],[25,111],[24,110]]
dict1 = {}
dict1['df1'] = pd.DataFrame(list1,index=pd.date_range('2000-1-1',periods=10, freq='D'), columns=list('AB'))
dict1['df2'] = pd.DataFrame(dict1['df1'] * (1-.05))
pan_so = pd.Panel(dict1)
pan_so = pan_so.transpose(2,1,0)

x = pan_so.ix[0,:,:]
def btwn(x):  # x is a dataframe
    y = x['df1'].rolling(center=False,window=6)
    z = x['df2'].rolling(center=False,window=6)
    x['cnt_btwn'] = pd.Series(pd.Series(y[:-1]).between(z[-1], y[-1], inclusive=True).sum())
    return x
btwn(x) 

What am I doing wrong? Thanks!

0

1 Answer 1

4

This y[:-1] makes an access to a Rolling object that doesn't support column indexing, that is the meaning of [:-1] in your code. You should apply a transformation function and get an actual series before filtering.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Boud - Possible to be a little more specific on the transformation function?
like mean max count etc. it s unclear to me why you put such [:-1] there and the previous question is heavy to read; what I do know though is that y is a Rolling object here and not a series so you cannot call that on that given location of the instruction
I want to compare "current" row's data to a rolling window which is [curr-6:curr-1], so the the [:-1] is just isolating the current row. Thank you for putting me on the right path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.