无法使用这些索引器对 <class 'pandas.core.indexes.period.PeriodIndex'> 进行切片索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49281713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:19:44  来源:igfitidea点击:

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

python-3.xpandas

提问by Gaurav

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'>

不能做切片索引对'<class 'pandas.core.indexes.period.PeriodIndex'>这些索引[2008q3]<'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')

这给了我错误: KeyError: ('2008q3', 'occurred at index 0')

回答by Scott Boston

Try:

尝试:

data.loc[:,'2008q3':'2009q2']

回答by Gaurav

Thanks @Scott for helping me out, After like trying a lot, i got it to work now.

感谢@Scott 帮助我,在尝试了很多之后,我现在可以使用它了。

I converted the data i had to DataFrame and then performed the above operation, it worked then.

我将我拥有的数据转换为DataFrame,然后执行上述操作,然后就可以了。

data = pd.DataFrame(housing)
data = data.loc[:,'2008q3':'2009q2']

data = data.reset_index()

data.columns = ['State', 'RegionName', '2008Q3', '2008Q4', '2009Q1', '2009Q2']
def price_ratio(difference):
return difference['2008Q3'] - difference['2009Q2']

data['Diff'] = data.apply(price_ratio,axis=1)

回答by Alex W

Transforming the columns like so:

像这样转换列:

data.columns = data.columns.astype(str)

will fix the issue. You can visualize the problem:

将解决问题。您可以将问题形象化:

With PeriodIndex:

使用周期索引:

>>> print(data.columns)

Index([2000Q1, 2000Q2, 2000Q3, 2000Q4, 2001Q1, 2001Q2, 2001Q3, 2001Q4, 2002Q1,
       2002Q2, 2002Q3, 2002Q4, 2003Q1, 2003Q2, 2003Q3, 2003Q4, 2004Q1, 2004Q2,
       2004Q3, 2004Q4, 2005Q1, 2005Q2, 2005Q3, 2005Q4, 2006Q1, 2006Q2, 2006Q3,
       2006Q4, 2007Q1, 2007Q2, 2007Q3, 2007Q4, 2008Q1, 2008Q2, 2008Q3, 2008Q4,
       2009Q1, 2009Q2, 2009Q3, 2009Q4, 2010Q1, 2010Q2, 2010Q3, 2010Q4, 2011Q1,
       2011Q2, 2011Q3, 2011Q4, 2012Q1, 2012Q2, 2012Q3, 2012Q4, 2013Q1, 2013Q2,
       2013Q3, 2013Q4, 2014Q1, 2014Q2, 2014Q3, 2014Q4, 2015Q1, 2015Q2, 2015Q3,
       2015Q4, 2016Q1, 2016Q2, 2016Q3],
      dtype='object')

After setting data.columns = data.columns.astype(str)

设置后 data.columns = data.columns.astype(str)

>>> print(data.columns)

Index(['2000Q1', '2000Q2', '2000Q3', '2000Q4', '2001Q1', '2001Q2', '2001Q3',
       '2001Q4', '2002Q1', '2002Q2', '2002Q3', '2002Q4', '2003Q1', '2003Q2',
       '2003Q3', '2003Q4', '2004Q1', '2004Q2', '2004Q3', '2004Q4', '2005Q1',
       '2005Q2', '2005Q3', '2005Q4', '2006Q1', '2006Q2', '2006Q3', '2006Q4',
       '2007Q1', '2007Q2', '2007Q3', '2007Q4', '2008Q1', '2008Q2', '2008Q3',
       '2008Q4', '2009Q1', '2009Q2', '2009Q3', '2009Q4', '2010Q1', '2010Q2',
       '2010Q3', '2010Q4', '2011Q1', '2011Q2', '2011Q3', '2011Q4', '2012Q1',
       '2012Q2', '2012Q3', '2012Q4', '2013Q1', '2013Q2', '2013Q3', '2013Q4',
       '2014Q1', '2014Q2', '2014Q3', '2014Q4', '2015Q1', '2015Q2', '2015Q3',
       '2015Q4', '2016Q1', '2016Q2', '2016Q3'],
      dtype='object')

You'll know it worked because data.loc['Texas'].loc['Austin'].loc['2002Q3']will work, instead of having to use data.loc['Texas'].loc['Austin'].loc[pd.Period('2002Q3')]

你会知道它有效,因为data.loc['Texas'].loc['Austin'].loc['2002Q3']会有效,而不是必须使用data.loc['Texas'].loc['Austin'].loc[pd.Period('2002Q3')]

if you need them in lowercase, e.g. 2001q3instead of 2001Q3:

如果您需要小写,例如2001q3而不是2001Q3

data.columns = list(map(str.lower, data.columns.astype(str)))