[英]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.