Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot do slice indexing on <class 'pandas.core.indexes.period.PeriodIndex'> with these indexers

file_location3 = "F:/python/course1_downloads/City_Zhvi_AllHomes.csv"

housing = pd.read_csv(file_location3)    
housing.set_index(['State','RegionName'],inplace=True)
housing = housing.iloc[:, 49:]

housing = housing.groupby(pd.PeriodIndex(housing.columns,freq='Q'),axis=1).mean()

data = housing
data = data.iloc[:,'2008q3' : '2009q2']

The error that I am getting is:

cannot do slice indexing on '<class 'pandas.core.indexes.period.PeriodIndex'> with these indexers [2008q3] of <'class 'str'>

Now I'm getting another error

def price_ratio(row):
    return (row['2008q3'] - row['2009q2']) / row['2008q3']

data['up&down'] = data.apply(price_ratio, axis=1)

This gives me error: KeyError: ('2008q3', 'occurred at index 0')

like image 548
Gaurav Avatar asked Mar 14 '18 15:03

Gaurav


1 Answers

Try:

data.loc[:,'2008q3':'2009q2']
like image 164
Scott Boston Avatar answered Sep 28 '22 18:09

Scott Boston