"AttributeError: 'Index' object has no attribute 'tz_localize'" while using the function history to extract stock information
"AttributeError: 'Index' object has no attribute 'tz_localize'" while using the function history to extract stock information
我正在尝试提取 Tesla 股票的信息,但在编码时总是出现错误。这是到目前为止的代码:
import plotly.graph_objects as go
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height= 900,
title= stock,
xaxis_rangeslider_visible=True)
fig.show()
Tesla= yf.Ticker('TSLA')
tesla_database= Tesla.history(period= "max")
在 tesla_database 部分出现以下错误:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
tesla_database= Tesla.history(period= "max")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/yfinance/base.py", line 295, in history
df.index = df.index.tz_localize("UTC").tz_convert(
AttributeError: 'Index' object has no attribute 'tz_localize'
我该如何解决这个问题?根据 Coursera 中的 IBM Watson 课程,这是正确的答案,但我总是遇到此错误。有人可以更正吗?
我在使用 pandas 1.4.1 时遇到了同样的问题 对我来说,回到以前的 pandas 版本解决了这个问题:
在命令行试试这个:
pip install pandas==1.2.2
在 Jupyter Board 中试试这个:
!pip install pandas==1.2.2
我正在尝试提取 Tesla 股票的信息,但在编码时总是出现错误。这是到目前为止的代码:
import plotly.graph_objects as go
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
from plotly.subplots import make_subplots
def make_graph(stock_data, revenue_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
fig.update_layout(showlegend=False,
height= 900,
title= stock,
xaxis_rangeslider_visible=True)
fig.show()
Tesla= yf.Ticker('TSLA')
tesla_database= Tesla.history(period= "max")
在 tesla_database 部分出现以下错误:
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
tesla_database= Tesla.history(period= "max")
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/yfinance/base.py", line 295, in history
df.index = df.index.tz_localize("UTC").tz_convert(
AttributeError: 'Index' object has no attribute 'tz_localize'
我该如何解决这个问题?根据 Coursera 中的 IBM Watson 课程,这是正确的答案,但我总是遇到此错误。有人可以更正吗?
我在使用 pandas 1.4.1 时遇到了同样的问题 对我来说,回到以前的 pandas 版本解决了这个问题:
在命令行试试这个:
pip install pandas==1.2.2
在 Jupyter Board 中试试这个:
!pip install pandas==1.2.2