gpt4 book ai didi

python - 尝试获取雅虎财务数据并将其设置为数据框时出现错误

转载 作者:行者123 更新时间:2023-12-01 07:33:37 25 4
gpt4 key购买 nike

当我尝试 for 循环来获取 yahoo 财务数据并将其设置为数据框时,我收到 KeyError: Date。此外,当我从 future 导入部门运行时,我也会遇到问题。任何人都可以帮忙吗?

import pandas as pd
from pandas import Series,DataFrame
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

from pandas_datareader import data

from datetime import datetime

tech_list = ['APPL','GOOG','MSFT','AMZN']

end = datetime.now()

start = datetime(end.year -1, end.month, end.day)
for stock in tech_list:
globals()[stock] = data.DataReader(stock,'yahoo',start,end)

当我输入 from future import division 时,出现错误

File "<ipython-input-19-69af1e1068cd>", line 4
SyntaxError: from __future__ imports must occur at the beginning of the file

当我输入for循环以获取雅虎财务数据并设置为数据框时

for stock in tech_list:
globals()[stock] = data.DataReader(stock,'yahoo',start,end)

我收到此错误

KeyError                                  Traceback (most recent call last)
F:\Anaconda3\Anaconda\lib\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/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
<ipython-input-23-6054597c5f52> in <module>
1 for stock in tech_list:
----> 2 globals()[stock] = data.DataReader(stock,'yahoo',start,end)

F:\Anaconda3\Anaconda\lib\site-packages\pandas_datareader\data.py in DataReader(name, data_source, start, end, retry_count, pause, session, access_key)
308 adjust_price=False, chunksize=25,
309 retry_count=retry_count, pause=pause,
--> 310 session=session).read()
311
312 elif data_source == "google":

F:\Anaconda3\Anaconda\lib\site-packages\pandas_datareader\base.py in read(self)
208 if isinstance(self.symbols, (compat.string_types, int)):
209 df = self._read_one_data(self.url,
--> 210 params=self._get_params(self.symbols))
211 # Or multiple symbols, (e.g., ['GOOG', 'AAPL', 'MSFT'])
212 elif isinstance(self.symbols, DataFrame):

F:\Anaconda3\Anaconda\lib\site-packages\pandas_datareader\yahoo\daily.py in _read_one_data(self, url, params)
140 prices.columns = [col.capitalize() for col in prices.columns]
141 prices['Date'] = to_datetime(
--> 142 to_datetime(prices['Date'], unit='s').dt.date)
143
144 if 'Data' in prices.columns:

F:\Anaconda3\Anaconda\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2925 if self.columns.nlevels > 1:
2926 return self._getitem_multilevel(key)
-> 2927 indexer = self.columns.get_loc(key)
2928 if is_integer(indexer):
2929 indexer = [indexer]

F:\Anaconda3\Anaconda\lib\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/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'Date'

最佳答案

错误是因为您正在寻找错误的库存。对于苹果来说,是 AAPL,而不是 APPL

import pandas as pd
from pandas_datareader import data
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

sns.set_style('whitegrid')
%matplotlib inline


tech_list = ['AAPL','GOOG','MSFT','AMZN']

end = datetime.now()
start = datetime(end.year -1, end.month, end.day)
stocks = {}

for stock in tech_list:
#storing each dataframe in as a key value pair or else it will
#overwrite the stock variable even if you define it as global var
stocks[stock] = data.DataReader(stock,'yahoo',start,end)

关于python - 尝试获取雅虎财务数据并将其设置为数据框时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57099766/

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