Python 尝试使用df.index.get\u loc(衰退\u开始)时返回一个键错误
我试图通过观察df列中连续值的趋势来找出衰退结束的时间。我想返回GDP连续两个季度增长的季度 我已经在以下功能中指出了衰退开始的时间:Python 尝试使用df.index.get\u loc(衰退\u开始)时返回一个键错误,python,pandas,jupyter-notebook,Python,Pandas,Jupyter Notebook,我试图通过观察df列中连续值的趋势来找出衰退结束的时间。我想返回GDP连续两个季度增长的季度 我已经在以下功能中指出了衰退开始的时间: def get_recession_start(): df = get_data() for i in range(1, len(df) - 1): if (df.iloc[i]['GDP'] < df.iloc[i - 1]['GDP']) and (df.iloc[i + 1]['GDP'] < df.iloc[i
def get_recession_start():
df = get_data()
for i in range(1, len(df) - 1):
if (df.iloc[i]['GDP'] < df.iloc[i - 1]['GDP']) and (df.iloc[i + 1]['GDP'] < df.iloc[i]['GDP']):
return df['Yearly quarters'].iloc[i]
get_recession_start()
我希望此函数返回单个字符串值“2008q3”,但是,我得到的是一条回溯消息:
KeyError回溯(最近一次呼叫最后一次)
//get_loc中的anaconda3/lib/python3.7/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2656尝试:
->2657返回发动机。获取位置(钥匙)
2658除键错误外:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi在pandas._libs.index.Int64Engine._check_type()中
键错误:“2008q3”
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在里面
11返回df['年度季度].iloc[i]
12
--->13经济衰退结束()
在经济衰退中
6 df=获取数据()
7衰退开始=获得衰退开始()
---->8索引=测向索引获取位置(衰退开始)
9表示范围内的i(索引+2,len(df)):
10如果(df.iloc[i]['GDP']>df.iloc[i-1]['GDP'])和(df.iloc[i-1]['GDP']>df.iloc[i-2]['GDP']):
//get_loc中的anaconda3/lib/python3.7/site-packages/pandas/core/index/base.py(self、key、method、tolerance)
2657返回发动机。获取位置(钥匙)
2658除键错误外:
->2659返回self.\u引擎。获取self.\u loc(self.\u可能\u cast\u索引器(键))
2660 indexer=self.get_indexer([key],method=method,tolerance=tolerance)
2661如果indexer.ndim>1或indexer.size>1:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi在pandas._libs.index.Int64Engine._check_type()中
键错误:“2008q3”
请注意,get\u recession\u start从“年度季度”列返回一个值
(从衰退开始的季度开始)
在recession\u start=get\u recession\u start()
中调用此函数,因此recession\u start
包含衰退开始的季度(类似于yyyqn)
然后您的代码包含index=df.index.get\u loc(衰退开始)
,所以您调用
获取_loc,传递刚刚找到的四分之一“名称”
这就是错误的来源,因为get_loc在索引中查找
(可能是一系列数字)仅用于此传递值(可能是2008q3)
显然,此索引不包含传递的值,因此异常
引发的只是关键错误:“2008q3”
要更正程序,请执行以下操作:
- 查找此行的键值
- 把它传给我
def get_recession_end():
import pandas as pd
import numpy as np
df = get_data()
recession_start = get_recession_start()
index = df.index.get_loc(recession_start)
for i in range(index + 2, len(df)):
if (df.iloc[i]['GDP'] > df.iloc[i-1]['GDP']) and (df.iloc[i -
1]['GDP'] > df.iloc[i - 2]['GDP']):
return df['Yearly quarters'].iloc[i]
get_recession_end()
I would expect this function to return the single string value '2008q3', however, instead I'm getting a traceback message:
KeyError Traceback (most recent call last)
//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2656 try:
-> 2657 return self._engine.get_loc(key)
2658 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
KeyError: '2008q3'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-16-5f3688935fbb> in <module>
11 return df['Yearly quarters'].iloc[i]
12
---> 13 get_recession_end()
<ipython-input-16-5f3688935fbb> in get_recession_end()
6 df = get_data()
7 recession_start = get_recession_start()
----> 8 index = df.index.get_loc(recession_start)
9 for i in range(index + 2, len(df)):
10 if (df.iloc[i]['GDP'] > df.iloc[i-1]['GDP']) and (df.iloc[i - 1]['GDP'] > df.iloc[i - 2]['GDP']):
//anaconda3/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2657 return self._engine.get_loc(key)
2658 except KeyError:
-> 2659 return self._engine.get_loc(self._maybe_cast_indexer(key))
2660 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2661 if indexer.ndim > 1 or indexer.size > 1:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()
KeyError: '2008q3'