<jupyter_start><jupyter_text><jupyter_code>pip install --upgrade pandas-datareader
import pandas_datareader as pdr # We are going to read the data with this specific library
import pandas as pd
from datetime import datetime
import matplotlib.pyplot as plt
%matplotlib inline
df_tesla=pdr.get_data_yahoo('TSLA')
df_tesla.head()
df_tesla.tail()
type(df_tesla)
plt.figure(figsize = (12, 4))
df_tesla.plot()
plt.show()
plt.figure(figsize = (12, 4))
df_tesla["High"].plot()
plt.show()
# x limit and y limit
plt.figure(figsize = (12, 4))
df_tesla["High"].plot(xlim = ["2020-01-01", "2021-09-29"])
plt.show()
# x limit and y limit
plt.figure(figsize = (12, 4))
df_tesla["High"].plot(xlim = ["2020-01-01", "2021-09-29"], ylim = [0, 1000])
plt.show()
# x limit and y limit
plt.figure(figsize = (12, 4))
df_tesla["High"].plot(xlim = ["2020-01-01", "2021-09-29"], ylim = [0, 1000], color = "red", linestyle = "-.", linewidth = 2.5)
plt.show()
df_tesla.index
index = df_tesla.loc["2017-02-27" : "2018-03-10"].index
share_open = df_tesla.loc["2017-02-27" : "2018-03-10"]["Open"]
index
share_open
fig, axs = plt.subplots()
plt.tight_layout() # In order to remove the overlapping of xlabels
fig.autofmt_xdate() # In order to remove the overlapping of xlabels
axs.plot(index, share_open)
# Date time index
df_tesla.info()
df_tesla = df_tesla.reset_index()
df_tesla.info()
pd.to_datetime(df_tesla["Date"])
# To set the index after resetting
df_tesla = df_tesla.set_index("Date", drop = True)
df_tesla.head()
# Date Time
datetime(2021,9,29)
datetime.now()<jupyter_output><empty_output><jupyter_text>Experimenting with datetime<jupyter_code>def add_num(num1, num2):
  return num1 + num2
start_time = datetime.now()
num1 = 10
num2 = 10
for i in range(1, 10):
  add_num(num1, num2)
end_time = datetime.now()
print(end_time - start_time)<jupyter_output>0:00:00.000225<jupyter_text>Continuing the project<jupyter_code>date = datetime(2021,9,29)
date
date.date()
date.day
date.weekday()
date.year
date.month<jupyter_output><empty_output><jupyter_text>Time Resampling<jupyter_code>df_tesla.head()
# Time Resampling
df_tesla.resample(rule = "A").min()
##Rule A == year end frequency
df_tesla.resample(rule = "A").max()["Open"].plot()<jupyter_output><empty_output><jupyter_text>Link for understanding different rules in resamplehttps://towardsdatascience.com/resample-function-of-pandas-79b17ec82a78<jupyter_code># Quarterly start frequency
df_tesla.resample(rule = "QS").max()["High"].plot()
# Business End Frequency
df_tesla.resample(rule = "BA").max()
df_tesla.resample(rule = "BQS").max()<jupyter_output><empty_output><jupyter_text>Plotting<jupyter_code>df_tesla["Open"].resample(rule = 'A').mean().plot(kind = "bar")
df_tesla["Open"].resample(rule = 'M').mean().plot(kind = "bar", figsize=(15,6))
df_tesla["High"].rolling(11).max().head(20)
df_tesla.head()
df_tesla["Open:30 days rolling"] = df_tesla["Open"].rolling(30).mean()
df_tesla.head(30)
df_tesla[["Open", "Open:30 days rolling"]].plot(figsize=(12, 5));<jupyter_output><empty_output><jupyter_text>Assignment1. Read the Microsoft Data using Pandas Data reader2. Get the maximum price of the share from 2017 to 20223. Which is the date of the highest price of the stock?4. Which is the date of the lowest price of the stock?<jupyter_code>df_mr = pdr.get_data_yahoo("MSFT")
df_mr.head()
df_mr.tail()
# xlim
df_mr["High"].plot(figsize = (12, 4))
df_mr["High"].plot(xlim = ["2020-09-29", "2021-09-29"], figsize = (12, 4))
# ylim
df_mr["High"].plot(xlim = ["2018-09-29", "2021-09-29"], ylim = [0, 350], figsize = (12, 4))
# Coloring
df_mr["High"].plot(xlim = ["2018-09-29", "2021-09-29"], ylim = [0, 350], figsize = (12, 4), color = "purple", linestyle = "--")
df_mr.index
index = df_mr.loc["2018-09-29": "2021-09-29"].index
share_open = df_mr.loc["2018-09-29": "2021-09-29"]["Open"]
share_open
index
figure, axs = plt.subplots()
plt.tight_layout()
figure.autofmt_xdate()
axs.plot(index, share_open)
plt.show()
## Datetime Index
df_mr = df_mr.reset_index()
df_mr
df_mr.info()
df_mr = df_mr.set_index("Date", drop = True)
df_mr.head()
## Time Resampling
df_mr.head()
df_mr.resample(rule = "A").min()
df_mr.resample(rule = "A").max()
##year end frequency
df_mr.resample(rule = "A").max()["Open"].plot()
##quaterly start frequency
df_mr.resample(rule = "QS").max()["High"].plot()
## Business End Frequency
df_mr.resample(rule = "BA").max()
df_mr.resample(rule = "BQS").max()
## Plotting
df_mr["Open"].resample(rule = "A").mean().plot(kind = "bar")
df_mr["Open"].resample(rule = "M").max().plot(kind = "bar", figsize = (15, 6))
df_mr["High"].rolling(11).max().head(20)
df_mr.head()
df_mr["Open: 30 days rolling"] = df_mr["Open"].rolling(30).mean()
df_mr.head(31)
df_mr[["Open", "Open: 30 days rolling"]].plot(figsize = (12, 5))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18649383232280994, 'fraction_numerical': 0.0633493623249007, 'mean_word_length': 6.281582952815829, 'pattern_counts': {'":': 2, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '460634', 'license': 'Apache-2.0', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1900, 'n_words': 384, 'path': 'Time_Series_EDA.ipynb', 'repo_name': 'Sukrut11/EDA-on-Tesla-and-Microsoft-Share-Prices', 'size': '4783'}