问题描述
菜鸟,但我正在学习,因此,如果这是一个简单的问题,请耐心等待...
当我尝试访问带有DatetimeIndex索引的数据帧中的特定行时,收到以下错误:DatetimeIndex格式的时间戳记值列表中的值:
*Traceback (most recent call last):
File "C:/TWS API/samples/Python/Testbed/earnings_processing.py",line 100,in <module>
print(names_histories_dicts[names_list[1]][earns_hists_dates_dicts[names_list_lower[1]][5]])
File "C:\TWS API\venv\lib\site-packages\pandas\core\frame.py",line 2800,in __getitem__
indexer = self.columns.get_loc(key)
File "C:\TWS API\venv\lib\site-packages\pandas\core\indexes\base.py",line 2648,in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx",line 111,in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx",line 138,in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi",line 1619,in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi",line 1627,in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: Timestamp('2019-05-01 00:00:00')*
列表和数据框索引都是使用
创建的pd.to_datetime(_______,format="%Y-%m-%d")
如果有人感兴趣,可以提供代码。
谢谢!
例如:
import pandas as pd
from datetime import datetime
# pd.set_option("display.max_rows",None,"display.max_columns",None)
# list of names
names = pd.read_csv('_________________.csv')
names_list = names['ticker label'].to_list()
#convert names list to lower case
names_list_lower = []
for i in range(len(names_list)):
names_list_lower.append(names_list[i].lower())
print(names_list_lower)
#create dictionary with names as keys to receive csv's
earns_hists_dicts = dict.fromkeys(names_list_lower)
#import name price histories from csv's
for i in names_list_lower:
csvpath = f"_______________{i}.csv"
earns_hists_dicts[i] = pd.read_csv(csvpath)
del earns_hists_dicts[i]['ticker']
del earns_hists_dicts[i]['companyshortname']
del earns_hists_dicts[i]['startdatetimetype']
del earns_hists_dicts[i]['timeZoneshortName']
del earns_hists_dicts[i]['gmtOffsetMilliSeconds']
del earns_hists_dicts[i]['quoteType']
earns_hists_dicts[i]['datetime_day'] = ""
for j in range(len(earns_hists_dicts[i])):
earns_hists_dicts[i]['datetime_day'][j] = earns_hists_dicts[i]['startdatetime'][j][:10]
earns_hists_dicts[i]['datetime_date'] = pd.to_datetime(earns_hists_dicts[i]['datetime_day'],format="%Y-%m-%d")
earns_hists_dicts[i] = earns_hists_dicts[i].set_index('datetime_date')
del earns_hists_dicts[i]['startdatetime']
earns_hists_dates_dicts = dict.fromkeys(names_list_lower)
for i in names_list_lower:
earns_hists_dates_dicts[i] = earns_hists_dicts[i]['datetime_day'].tolist()
for i in names_list_lower:
earns_hists_dates_dicts[i] = pd.to_datetime(earns_hists_dates_dicts[i],format="%Y-%m-%d")
#create dictionary with names as keys to receive csv's
names_histories_dicts = dict.fromkeys(names_list,[])
#import name price histories from csv's
for i in names_list:
csvpath = f"_________________________{i}.csv"
names_histories_dicts[i] = pd.read_csv(csvpath)
names_histories_dicts[i]['datetime_date'] = pd.to_datetime(names_histories_dicts[i]['Date'],format="%Y-%m-%d")
names_histories_dicts[i] = names_histories_dicts[i].set_index('datetime_date')
print(type(names_histories_dicts[i].index))
print(names_histories_dicts[names_list[1]][earns_hists_dates_dicts[names_list_lower[1]][5]])
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)