gpt4 book ai didi

python - pandas 阻止我下载我不想拥有的文件

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

所以我正在学习金融和 matplotlib,我想下载 20 个 .csv 文件,每个文件都包含一些数据。问题是当我执行代码时,终端打印出一个巨大的错误。

到目前为止,我已经尝试更改代码,尝试从.pickle文件中删除MSTAR,即使MSTAR甚至不在.pickle文件中,我也尝试在Github上询问,仍然没有,我甚至在堆栈溢出上查找它,但没有任何效果

当我运行以下命令时似乎会发生错误

def get_data_from_yahoo(reload_sp500=False):
if reload_sp500:
tickers = save_sp500_tickers()
else:
with open("sp500tickers.pickle", "rb") as f:
tickers = pickle.load(f)
if not os.path.exists('stock_dfs'):
os.makedirs('stock_dfs')

start = dt.datetime(2010, 1, 1)
end = dt.datetime.now()
for ticker in tickers[:20]:

if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
df = web.DataReader(ticker, 'morningstar', start, end)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df = df.drop("Symbol", axis=1)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))

get_data_from_yahoo()

我期望一个充满 .csv 文件的文件夹,每个文件都有其数据,但我收到一条错误消息,告诉我以下内容:

Traceback (most recent call last):
File "C:\Users\chedl\Documents\Programs\Python\financing\python-for-financing.py", line 46, in <module>
get_data_from_yahoo()
File "C:\Users\chedl\Documents\Programs\Python\financing\python-for-financing.py", line 37, in get_data_from_yahoo
df = web.DataReader(ticker, 'morningstar', start, end)
File "C:\Users\chedl\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas_datareader\data.py", line 387, in DataReader
session=session, interval="d").read()
File "C:\Users\chedl\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\pandas_datareader\mstar\daily.py", line 57, in __init__
raise ImmediateDeprecationError(DEP_ERROR_MSG.format("Morningstar"))
pandas_datareader.exceptions.ImmediateDeprecationError:
Morningstar has been immediately deprecated due to large breaks in the API without the
introduction of a stable replacement. Pull Requests to re-enable these data
connectors are welcome.

如果你不明白这个错误,它会要求一个特定的数据集,即使我不想要它,它会尝试下载该数据集,然后它会提示 API 中的一些中断,即使我从来不想要这个文件,而且,它从 .pickle 文件中获取名称,嗯,该 .pickle 文件中没有写到 Morningstar 或 MSTAR。

最佳答案

看起来您在代码中专门向晨星请求数据:

df = web.DataReader(ticker, 'morningstar', start, end)

与雅虎财经合作有时会相当困难。我发现 Quandl 更易于使用且更可靠。您可以免费注册here这将允许您访问 API key 。他们还有一个 python 库,您可以 pip 安装它,然后导入到您的文件中。从那里,只需将对 Yahoo Finance 的调用替换为对 Quandl 的调用即可。

    if not os.path.exists('stock_dfs/{}.csv'.format(ticker)):
quandl.ApiConfig.api_key = "YOUR API KEY"
df = quandl.get(ticker, start_date=start, end_date=end)
df.reset_index(inplace=True)
df.set_index("Date", inplace=True)
df = df.drop("Symbol", axis=1)
df.to_csv('stock_dfs/{}.csv'.format(ticker))
else:
print('Already have {}'.format(ticker))

Quandl 文档 here

关于python - pandas 阻止我下载我不想拥有的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57682204/

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