GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — SimpleStockAnalysisPython/Python_02_Modify_Date.py at master · qalpha/SimpleStockAnalysisPython

Expand file tree

/

Python_02_Modify_Date.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
150 lines (107 loc) · 3.35 KB
/

Python_02_Modify_Date.py

Copy path

File metadata and controls

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