Skip to content

Commit ec6890d

Browse files
Add files via upload
1 parent b48822f commit ec6890d

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

Python_1_Dowload__YahooData.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 27 08:09:11 2020
4+
5+
@author: Tin
6+
"""
7+
# Download stock data from Yahoo Finance
8+
9+
import yfinance as yf
10+
yf.pdr_override()
11+
12+
13+
# input
14+
symbol = 'AAPL'
15+
start = '2014-01-01'
16+
end = '2018-01-01'
17+
18+
19+
# dataframe
20+
df = yf.download(symbol,start,end)
21+
22+
# View the first 5 rows
23+
print('First 5 Rows')
24+
print(df.head())
25+
print('-'*80)
26+
27+
# View the last 5 rows
28+
print('Last 5 Rows')
29+
print(df.tail())

Python_2_Modify_Date.py

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Fri Nov 27 08:09:11 2020
4+
5+
@author: Tin
6+
"""
7+
# Modify Yahoo Dataframe Date
8+
import pandas as pd # Dataframe Library
9+
pd.set_option('max_columns', None) # To show all columns
10+
11+
import yfinance as yf
12+
yf.pdr_override()
13+
14+
15+
# input
16+
symbol = 'AAPL'
17+
start = '2014-01-01'
18+
end = '2018-01-01'
19+
20+
21+
# dataframe
22+
data = yf.download(symbol,start,end)
23+
24+
# View the first 5 rows
25+
print('First 5 Rows')
26+
print(data.head())
27+
print('-'*80)
28+
29+
30+
# Date becomes a columns
31+
df = data.copy() # Copy the original data
32+
dfn = df.reset_index()
33+
print(dfn.head())
34+
print('-'*80)
35+
36+
37+
# Add Year, Month, Day
38+
df['Year'] = df.index.year
39+
df['Month'] = df.index.month
40+
df['Day'] = df.index.day
41+
print('Year, Month, & Day')
42+
print(df.head())
43+
print('-'*80)
44+
45+
46+
# Convert Daily to Weekly
47+
weekly = data.copy()
48+
weekly = weekly.resample('W').last()
49+
print('Weekly Data')
50+
print(weekly.head())
51+
print('-'*80)
52+
53+
54+
# Convert Daily to Monthly
55+
monthly = data.copy()
56+
monthly = monthly.resample('1M').mean()
57+
print('Monthly Data')
58+
print(monthly.head())
59+
print('-'*80)
60+
61+
62+
# Choose Particular Year to analyze
63+
monthly = data.copy()
64+
monthly = monthly.reset_index()
65+
y2017 = monthly[monthly['Date'].dt.year==2017]
66+
print("Analyze Particular Year in Historical Data")
67+
print(y2017)
68+
print('-'*80)
69+
70+
71+
month_name = data.copy()
72+
# Convert Daily to Monthly
73+
# 'BMS', which stands for "business month start frequency"
74+
# 'BM', which stands for "business month end frequency"
75+
month_name = month_name.asfreq('BM')
76+
print('Number of the Month')
77+
print(month_name.head())
78+
print('-'*80)
79+
80+
81+
import calendar
82+
month_name['Month_Number'] = month_name.index.month
83+
month_name['Month_ABBR'] = month_name['Month_Number'].apply(lambda x: calendar.month_abbr[x])
84+
print('Abbreviation for Months')
85+
print(month_name.head())
86+
print('-'*80)
87+
88+
89+
print('Month Name')
90+
month_name['Month_Name'] = month_name['Month_Number'].apply(lambda x: calendar.month_name[x])
91+
print(month_name.head())
92+
print('-'*80)
93+
94+
95+
# Pivot Table Date
96+
df_months = pd.pivot_table(df, index=df.index.month, columns=df.index.year, values = 'Adj Close') # each months
97+
print('Year by Year')
98+
print(df_months)
99+
print('-'*80)
100+
101+
102+
df_days = pd.pivot_table(df, index=df.index.day, columns=df.index.year, values = 'Adj Close') # daily for one whole months
103+
print('Year by Year in daily rows')
104+
print(df_days)
105+
print('-'*80)
106+
107+
108+
df_all_columns = pd.pivot_table(df, index=df.index.month, columns=df.index.year)
109+
print('All columns in yearly')
110+
print(df_all_columns)
111+
print('-'*80)
112+
113+
114+
stock_data = df.copy()
115+
stock_data['Year'] = df.index.year
116+
stock_data['Month'] = df.index.month
117+
stock_data['Day'] = df.index.day
118+
stock_data['Week_Day'] = df.index.dayofweek
119+
stock_data['Week_Day_Name'] = df.index.strftime('%A')
120+
print('Number of day with M-F')
121+
print(stock_data.tail(10))
122+
print('-'*80)
123+
124+
125+
approach1 = stock_data.groupby(['Year', 'Month']).first()['Adj Close']
126+
print('# of Month')
127+
print(approach1.tail(12))
128+
print('-'*80)
129+
130+
131+
approach2 = stock_data.groupby(['Year', 'Day']).first()['Adj Close']
132+
print('# of Day')
133+
print(approach2.tail(12))
134+
print('-'*80)
135+
136+
137+
print('Convert Date to String')
138+
string_date = data.copy()
139+
string_date['Date'] = string_date.index
140+
print(string_date.head())
141+
print('-'*80)
142+
143+
144+
string_date['Date'] = string_date['Date'].dt.strftime("%Y%m%d").astype(int)
145+
print('Convert Date to Numbers')
146+
print(string_date.head())
147+
148+
149+
150+

0 commit comments

Comments
 (0)