gpt4 book ai didi

python - Pandas index.get_loc 给出时间序列数据的 keyerror

转载 作者:行者123 更新时间:2023-12-01 02:02:19 28 4
gpt4 key购买 nike

下面的代码工作正常。

import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SPY'
df_close = pd.DataFrame()
df_temp = pd.read_json('https://api.iextrading.com/1.0/stock/'+sym+'/chart/5y')
df_temp.set_index('date',inplace=True)
df_close = df_temp['close']

loc = df_close.index.get_loc('2015-08-17')

我修改了它以从 nsepy 包中获取数据。替换了 read_json 行并注释了 set_index 行,因为从包中获取的数据默认将日期行作为索引

import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
from datetime import date
from nsepy import get_history

get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SBIN'
df_close = pd.DataFrame()
df_temp = get_history(symbol=sym,
start=date(2014,1,1),
end=date(2018,3,24))
#df_temp.set_index('date',inplace=True)
df_close = df_temp['Close']

loc = df_close.index.get_loc('2015-08-17')

在这两种情况下,df_close 都是一个系列,并且其中都有日期。唯一的区别是,在正确的情况下,它包含格式如下的日期2013-03-25 00:00:00

而在错误的格式中,其格式类似于2014-01-01

这是日志。

runfile('C:/Users/Arun/.spyder-py3/Practise files/market_correction.py', wdir='C:/Users/Arun/.spyder-py3/Practise files') Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/Arun/.spyder-py3/Practise files/market_correction.py', wdir='C:/Users/Arun/.spyder-py3/Practise files')

File "C:\Users\Arun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\Arun\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/Arun/.spyder-py3/Practise files/market_correction.py", line 27, in loc = df_close.index.get_loc('2015-08-17')

File "C:\Users\Arun\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2527, in get_loc return self._engine.get_loc(self._maybe_cast_indexer(key))

File "pandas/_libs/index.pyx", line 117, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/index.pyx", line 139, in pandas._libs.index.IndexEngine.get_loc

File "pandas/_libs/hashtable_class_helper.pxi", line 1265, in pandas._libs.hashtable.PyObjectHashTable.get_item

File "pandas/_libs/hashtable_class_helper.pxi", line 1273, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: '2015-08-17'

我做错了什么?这一天出现在该系列中。

我也尝试过 df.loc 方法,但这会产生其他错误。

我正在使用 anaconda spyder 和 python 3.6

解决方案:

import pandas as pd
#import numpy as np
import matplotlib.pyplot as plt
from IPython import get_ipython
from datetime import date
from nsepy import get_history

get_ipython().run_line_magic('matplotlib', 'inline')

sym = 'SBIN'
df_close = pd.DataFrame()
df_temp = get_history(symbol=sym,
start=date(2014,1,1),
end=date(2018,3,24))

**df_temp.reset_index(drop = False, inplace = True)
df_temp['Date']= pd.to_datetime(df_temp['Date'])
df_temp.set_index('Date',inplace=True)**
df_close = df_temp['Close']

loc = df_close.index.get_loc('2015-08-17')

最佳答案

我认为需要set_index并且可能转换为日期时间,因为 KeyError 意味着索引中没有值 2015-08-17:

#check if DatetimeIndex
print (df_temp.index)

#if necessary convert column to index
df_temp.set_index('date',inplace=True)
#if necessary convert to datetimes
df_temp.index= pd.to_datetime(df_temp.index)

loc = df_temp.index.get_loc('2015-08-17')

关于python - Pandas index.get_loc 给出时间序列数据的 keyerror,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472835/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com