-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweekAvg.py
More file actions
42 lines (36 loc) · 968 Bytes
/
weekAvg.py
File metadata and controls
42 lines (36 loc) · 968 Bytes
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
import csv
import pandas
from datetime import datetime
def dow(dstr):
dateArr = dstr.split('/')
return datetime(int(dateArr[0]), int(dateArr[1]), int(dateArr[2])).weekday()
def toPrecision2(n):
return "%0.2f" % (n)
def avgNumberArray(arr, days):
sumN = 0
for i in arr:
sumN = i + sumN
return toPrecision2(sumN / days)
fname = './ch06_tsmc.csv'
f = open(fname,"r")
sumReturns = [0.0, 0.0, 0.0, 0.0, 0.0]
cntReturns = [0, 0, 0, 0, 0]
result = []
index = 0
for line in f:
if index > 0:
strList = line.split(',')
wkday = dow(strList[0])
re = float(strList[5])
if wkday > 4:
wkday = wkday % 5
sumReturns[wkday] = re
cntReturns[wkday] = cntReturns[wkday] + 1
index = index + 1
for index, su in enumerate(sumReturns):
result.append(su / cntReturns[index])
print(result)
data = pandas.Series(result, index=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"])
data.plot()
import matplotlib.pyplot as plt
plt.show()