MJS MJS -3 years ago 412
Python Question

python pandas- AttributeError: 'Series' object has no attribute 'columns'?

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

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

What am I doing wrong? Thanks!

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download