I'm trying to extract information of the Tesla stock but, I always get the error while coding it. Here's the code so far:
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")
In the tesla_database part is where the following error comes:
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'
How can I fix this? According to the IBM Watson Course in Coursera, this is the correct answer but I get this error all the time. Can someone correct it, please?
I had the same issue with pandas 1.4.1 For me reverting back to a previous pandas version solved the problem:
Try this on command line:
pip install pandas==1.2.2
Try this from whithin Jupyter Board:
!pip install pandas==1.2.2