简体   繁体   English

在 GOOGLE COLAB 中运行此 python 代码时,它向我显示错误。 谁能告诉我我做错了什么并分享更正的代码?

[英]On running this python code in GOOGLE COLAB, it showing me error. Can anyone please tell what am i doing wrong and share a corrected code?

import pandas_datareader.data as web import numpy as np import pandas as pd stock = ['AAPL'] data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close'] data.sort_index(inplace=True) returns = data.pct_change() mean_return = returns.mean() return_stdev = returns.std() annualised_return = round(mean_return * 252,2) annualised_stdev = round(return_stdev * np.sqrt(252),2) print ('The annualised mean return of stock {} is {}, ' 'and the annualised volatility is {}').format(stock[0],annualised_return,annualised_stdev)

You're calling format on the output of the print function by putting it after the close parenthesis.您通过将其放在右括号之后来调用print函数输出的format print doesn't return anything, so you're effectively calling None.format(...) (which doesn't exist). print不返回任何内容,因此您实际上是在调用None.format(...) (不存在)。 You should instead call format on the string directly as such:您应该直接在字符串上调用 format ,如下所示:

print('The annualised mean return of stock {} is {}, and the annualised volatility is {}'.format(stock[0],annualised_return,annualised_stdev))

Here is the corrected code:这是更正后的代码:

import pandas_datareader.data as web

import numpy as np 

import pandas as pd

stock = ['AAPL']

data = web.DataReader(stock,data_source="yahoo",start='01/01/2010')['Adj Close'] 
data.sort_index(inplace=True)

returns = data.pct_change() 

mean_return = returns.mean()

return_stdev = returns.std() 

annualised_return = round(mean_return * 252,2)

annualised_stdev = round(return_stdev * np.sqrt(252),2)

print(f"The annualised mean return of stock {stock[0]} is {annualised_return}, and the annualised volatility is {annualised_stdev}")

So the error came because you did not use af string.所以错误是因为你没有使用 af 字符串。 An f string puts data in-between the brackets{} f 字符串将数据放在方括号之间{}

For more info visit: https://www.geeksforgeeks.org/python-output-formatting/#:~:text=In%20Python%2C%20there%20is%20no%20printf%20%28%29%20function,string%20modulo%20%28or%20sometimes%20even%20called%20modulus%29%20operator .更多信息请访问: https : //www.geeksforgeeks.org/python-output-formatting/# :~: text=In%20Python%2C%20there%20is%20no%20printf%20%28%29%20function,string %20modulo%20%28or%20sometimes%20even%20 called%20modulus%29%20operator

Release notes for pandas_datareader 0.10.0 does mention that there were issues with Yahoo API's requiring headers and were fixed in this release (0.10.0). pandas_datareader 0.10.0 的发行说明确实提到了Yahoo API需要标头的问题,并在此版本 (0.10.0) 中得到了修复。

Fixed Yahoo readers which now require headers修复了现在需要标题的雅虎阅读器

So, if your google-colab uses any version older than 0.10.0 will have problems using Yahoo API's .因此,如果您的google-colab使用任何低于 0.10.0 的版本,在使用Yahoo API 的.

Here are some steps on how to debug the issue in google-colab - jupyter notebook .以下是有关如何在google-colab - jupyter notebook调试问题的一些步骤。
Step 1: Determine the installed version of module pandas_datareader .步骤 1:确定已安装的模块pandas_datareader版本。

!pip show pandas_datareader

Step 2: if the version is lower than 0.10.0 then upgrade the version.第二步:如果版本低于0.10.0则升级版本。

!pip install --upgrade pandas_datareader

Step 3: Don't forget to restart the runtime to load the new libraries.第 3 步:不要忘记重新启动运行时以加载新库。

Press Runtime->Restart runtime

Step 4: Now try running the step 1 again to determine if the newest version is installed.第 4 步:现在尝试再次运行第 1 步以确定是否安装了最新版本。

!pip show pandas_datareader

I hope the newest version will be installed and you can run your above code with the correction mentioned by @ Aaron in his answer.我希望安装最新版本,您可以使用@ Aaron在他的回答中提到的更正来运行上面的代码。

Note: !注意: ! is required before shell commands.在 shell 命令之前是必需的。 Try going thorugh the official docs of IPython for more info.尝试通过 IPython 的官方文档了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不明白为什么这段代码不起作用! 有人可以告诉我我做错了吗? - I don't see why this code is not working! can someone please tell me what i am doing wrong? 谁能告诉我我做错了什么,字典与 Python 中的列表? - Can anyone tell me what I am doing wrong, dictionary vs list in Python? Python-比较数字。 我正在将1与1比较,但失败了-请查看代码-有人可以告诉我这是什么问题吗? - Python - comparing numbers. I am comparing 1 with 1 but it is failing - please see code - Can anyone tell me what is the issue? 有人可以告诉我我做错了什么新到 Python - Can someone please tell me what am I doing wrong New to Python 有人可以告诉我这个 python 代码哪里错了吗? - Can someone please tell me where am I wrong in this python code? 谁能向我解释以下代码有什么问题 - Can anyone please explain to me what is wrong with the following code 谁能告诉我这里的代码有什么问题? - Can anyone tell me what's wrong with my code here? 有人可以告诉我我做错了什么吗? - Can someone tell me what am I doing wrong? 谁能告诉我这个 CNN 代码中的错误是什么? - Can anyone tell me what is the error in this CNN code? (python)你能告诉我下面代码中有什么问题吗 - (python) Can you please tell me what is the problem in the code below
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM